[wp-trac] [WordPress Trac] #19572: switch_to_post() stack implementation (similar to switch_to_blog())

WordPress Trac wp-trac at lists.automattic.com
Thu Dec 15 21:54:57 UTC 2011

#19572: switch_to_post() stack implementation (similar to switch_to_blog())
 Reporter:  alexkingorg  |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:  3.3
 Severity:  normal       |   Keywords:  has-patch
 One of the challenges themes and plugins have is the balance between
 utilizing the core APIs that rely on global variables, while leaving
 things in a "clean" state for the next worker in the chain.

 For example, I might have a shortcode that pulls content from another
 post, outputs a title or some other data, then needs to restore the
 previous post environment. The attached patch provides APIs for doing just
 this - using the same stack approach that switch_to_blog() takes in a
 multi-site instance.

 To set up a post context, call: switch_to_post($post_id);

 This can be called multiple times, diving deeper into the stack. Then each
 process is responsible for restoring the $post context by calling:

 This goes back to the stack, bumps out the current post and restores the
 previous post's context.

 There is some test code for demonstration here:


Ticket URL: <http://core.trac.wordpress.org/ticket/19572>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software

More information about the wp-trac mailing list