0

I need to switch WordPress's post context, sort of like switch_to_blog() but for posts. What's the safest way to alter WordPress's global state and switch it back?

1 Answer 1

2

Use setup_postdata() to change and wp_reset_postdata() to roll back to original.

2
  • Very helpful, thanks Rarst. I don't need to loop through any posts. Just get a single one via get_post( $post_id ). Is this still the way to go given that background? I'm not using have_post(), the_post() and the like, just get_post(). (Googlers, see also: codex.wordpress.org/Class_Reference/…)
    – editor
    Jun 29, 2011 at 16:57
  • @editor yep, same principle. The point of setup_postdata() is that it doesn't care about the Loop or more precisely it's one of small standalone cogs in it.
    – Rarst
    Jun 29, 2011 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.