Skip to content

Instantly share code, notes, and snippets.

@alexkingorg
Created October 19, 2011 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexkingorg/1297154 to your computer and use it in GitHub Desktop.
Save alexkingorg/1297154 to your computer and use it in GitHub Desktop.
switch_to_post() stack implementation (similar to switch_to_blog()) for WordPress
<?php
$WP_POST_STACK = array();
function switch_to_post($post_id) {
global $WP_POST_STACK, $post;
if (!count($WP_POST_STACK)) {
$WP_POST_STACK[] = $post->ID;
}
$WP_POST_STACK[] = $post_id;
$post = get_post($post_id);
setup_postdata($post);
}
function restore_post() {
global $WP_POST_STACK;
if (count($WP_POST_STACK)) {
$post_id = array_pop($WP_POST_STACK);
$post = get_post($post_id);
setup_postdata($post);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment