| 1 | <?php
|
|---|
| 2 |
|
|---|
| 3 | $WP_POST_STACK = array();
|
|---|
| 4 |
|
|---|
| 5 | function switch_to_post($post_id) {
|
|---|
| 6 | global $WP_POST_STACK, $post;
|
|---|
| 7 |
|
|---|
| 8 | // Initialize the stack with very first $post->ID
|
|---|
| 9 | if ( !count( $WP_POST_STACK ) ) {
|
|---|
| 10 | $WP_POST_STACK[] = $post->ID;
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | // Stick our new post_id onto the end
|
|---|
| 14 | $WP_POST_STACK[] = $post_id;
|
|---|
| 15 | $post = get_post( $post_id );
|
|---|
| 16 | setup_postdata( $post );
|
|---|
| 17 | do_action( 'switch_to_post', $post_id );
|
|---|
| 18 |
|
|---|
| 19 | return true;
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | function restore_post() {
|
|---|
| 23 | global $WP_POST_STACK, $post;
|
|---|
| 24 |
|
|---|
| 25 | // If we don't have anything to knock off, just return
|
|---|
| 26 | if ( count( $WP_POST_STACK ) ) {
|
|---|
| 27 | // Remove the current post_id off the stack
|
|---|
| 28 | $removed_post_id = array_pop( $WP_POST_STACK );
|
|---|
| 29 |
|
|---|
| 30 | // Get last value of the array but leave it on the array
|
|---|
| 31 | if ( $post_id = end( $WP_POST_STACK ) ) {
|
|---|
| 32 | $post = get_post( $post_id );
|
|---|
| 33 | setup_postdata( $post );
|
|---|
| 34 | do_action( 'restore_post', $post_id, $removed_post_id );
|
|---|
| 35 | return true;
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 | return false;
|
|---|
| 39 | }
|
|---|