[wp-hackers] Resetting after multiple loops

Otto otto at ottodestruct.com
Mon Oct 27 13:23:56 GMT 2008

On Mon, Oct 27, 2008 at 4:25 AM, Nigel Dunn <nigel at redefine.co.uk> wrote:
> would bring the $post and the $id globals back to their previous values (and
> any others).

No, *nothing* will bring the $post and $id globals back to their
previous values. There is no way to do that.

Nor should there be any way to do that, because those globals are only
valid values within the context of the Loop itself. If you're
expecting to be able to use a non-Loop function outside the Loop,
well, then this is the sort of problem you will encounter.

Don't use the_title() or the_content() or any Loop function outside of
an actual Loop... ever.

> Or I'd imagine the alternative would be to place that at the start of any
> widget to make sure that it was dealing with the post being viewed. I had
> been taking a different approach, in that anything that changed the globals
> should clean up after itself.

You're thinking that the globals should be reset, but the question
really is, reset *to what*? It doesn't make any sense to reset those,
because they are Loop globals. Outside the Loop their values are
undefined. So if you're using them, you're relying on a side effect,
which is obviously never a good thing to do.

In other words, what in the heck are you doing AFTER your loop that
requires those values in the first place? That's where the problem is.
You shouldn't be doing whatever that is.


More information about the wp-hackers mailing list