Have at least five men per meter of front line.
Trenches remain useful and widely used in the modern age of missiles, drones, and precision guided artillery. However, to get the trappings of WWI trench warfare - long, mostly static fronts of opposing layered trenchworks - you need enough men to hold that much territory, and that number, historically, is staggeringly huge. Spanning a few hundred kilometers of the Western Front took armies of millions, with some individual battles involving millions of men. The biggest reason why you don't see extended trenchworks in modern conflicts isn't modern technology, it's the fact that trench warfare is totally mobilized warfare, and modern total war is nuclear war.
Which brings us to:
It's total war, but nobody can use nukes for some reason.
You'll probably have to dip into the suspension of disbelief a little bit for this one, one way or another. Some sort of technologically themed magic nonsense would be my suggestion.
Now we have the necessary preconditions to have trenches at all, we need to make them useful, so we must:
Make area-targeting artillery the king of the battlefield, and give infantry the tools to protect it.
Infantry protects artillery from infantry. Trenches protect infantry from artillery. If artillery is king, infantry is queen, and if infantry and artillery rule the battlefield together, they have layered trenches in the front protecting artillery fortifications in the back. This means pushing down the usefulness of air power (including orbital strikes and long range missiles), precision guided munitions, and armored cavalry (tanks, etc).
We can get rid of armored cav by giving infantry man-portable direct fire artillery that can put holes in tanks. It has to be reach-out-and-touch-it weaponry of some kind, not something like a modern man-portable missile launcher, otherwise tanks will be able to beat anti-tank infantry to the punch, and will disrupt our perfect trench paradise.
Getting rid of air power is harder, since we need artillery shells, which go in the air. Maybe anti-aircraft and anti-missile missiles are crazy effective but both sides' supplies are low, so we only use them to shoot down valuable targets, not thousands of dumb shells fired by artillery.
Note that both sides need to have these capabilities, and they need to have artillery with the same effective range. If either side's artillery can kill the enemy artillery from farther away than it can shoot back, or if either side has armor or air that can punch through or over trench lines and kill the enemy artillery, you'll have guerilla or urban warfare, not trench warfare, regardless of any other factors.
Now that artillery and infantry are fighting it out with millions (maybe billions in scifi) of men and no nukes, highly effective tanks, or highly effective bombers to ruin our fun, we need to do some research about:
How actual historical trench warfare worked
The below is an extreme oversimplification by me, a non-expert!
The main mistake that fiction makes is making it seem that the defender has the advantage in trench warfare. The exact opposite is true. The reason why trench warfare is a stalemate is that winning an engagement as the attacker means occupying the enemy trench, and the enemy knows this in advance and gets to position their artillery and back-up trenches in such a way as to make their own trench a place where they can counterattack and kill you.
It evolves something like this: artillery will absolutely 100% murder the pants off your infantry if you have to cross more than a few hundred yards to get to safety, and then their infantry will murder your artillery. You dig a trench to prevent this, and their artillery can't approach too close or they'll get pounded by your artillery, so they set up their artillery just out of range of your artillery and slowly advance their own trenchworks until they can't go any closer without being killed by sorties by your infantry.
If the enemy artillery can do a decent job of suppressing your infantry, their infantry can cross the no-man's-land and murder your infantry before your artillery can kill enough of them, because a trench that the enemy is at the top of is just a grave that you dug for yourself to die in. Then they can then hide in your trench to make your artillery stop killing them.
However, their artillery has to stop shooting or else they'll just blow up their own infantry while you're hiding in your nice trench. If you can rush back to the line with your machine guns after the barrage ends, you can machine-gun all their infantry while they're crossing no-man's land. So they try to time it as close as possible, and you try to slow them down with barbed wire, interconnected fall-back trenches that you can retreat to during the barrage, and so on. However, sufficiently overwhelming and tightly timed artillery fire can give the attacker a better chance of getting there first.
You know this problem, so you dig an extra trench line behind your trench line. Inevitably, they will drive you back, but doing so will cause their infantry to advance into range of your artillery. So as soon as they finish winning the battle for your front trench line, they're in a horrible, exposed position. As soon as your infantry is ready to counter-attack, you shell the heck out of them (they hide in your trench), advance your infantry at the double (no barbed wire slowing them down behind your front line!) and your infantry murders them in your trench, re-occupies your trench, and everyone's right back where they started, except a lot of people are dead.