Created
November 15, 2018 11:05
-
-
Save manchumahara/76c1b7348a4a9503b642f0a6382112b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Setup a post object and store the original loop item so we can reset it later | |
* | |
* @param obj $post_to_setup The post that we want to use from our custom loop | |
*/ | |
function setup_admin_postdata( $post_to_setup ) { | |
//only on the admin side | |
if ( is_admin() ) { | |
//get the post for both setup_postdata() and to be cached | |
global $post; | |
//only cache $post the first time through the loop | |
if ( ! isset( $GLOBALS['post_cache'] ) ) { | |
$GLOBALS['post_cache'] = $post; | |
} | |
//setup the post data as usual | |
$post = $post_to_setup; | |
setup_postdata( $post ); | |
} | |
} | |
/** | |
* Reset $post back to the original item | |
* | |
*/ | |
function wp_reset_admin_postdata() { | |
//only on the admin and if post_cache is set | |
if ( is_admin() && ! empty( $GLOBALS['post_cache'] ) ) { | |
//globalize post as usual | |
global $post; | |
//set $post back to the cached version and set it up | |
$post = $GLOBALS['post_cache']; | |
setup_postdata( $post ); | |
//cleanup | |
unset( $GLOBALS['post_cache'] ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment