Restores the global post to its previous value after preparing a revision.
Description
Preparing a revision overwrites the global post and post data via setup_postdata(). This restores the global post that was in place beforehand so the change does not leak into the rest of the request. Only the global post is guaranteed to be restored. When there was no previous global post and the main query has no post either, which is the usual state during a REST request, wp_reset_postdata() has nothing to restore from, so the remaining globals set by setup_postdata() (such as $id, $authordata and $pages) are left describing the revision. Clearing those would mean unsetting each one by hand, which is beyond what is needed to keep the global post from leaking.
Parameters
$previous_postWP_Post|null
The global post to restore, or null if there was none.
765privatefunctionrestore_post_data(?WP_Post$previous_post):void{766if($previous_post){767$GLOBALS['post']=$previous_post;768setup_postdata($previous_post);769return;770}771772/*773 * There was no global post to restore, so clear the revision's post data.774 * This runs before clearing the global post because wp_reset_postdata()775 * repopulates it from the main query whenever that query has a post. Note776 * that it is a no-op when the main query has no post, in which case only777 * the global post below is cleared.778 */779wp_reset_postdata();780781/*782 * Assigned rather than unset so that any `global $post` binding made before783 * this request keeps pointing at the global. Unsetting removes the entry from784 * the symbol table, which detaches those bindings, and a later write through785 * one of them would no longer be visible to get_post().786 */787$GLOBALS['post']=null;788}
History
Introduced in 7.1.0.
Signature, return type and hooks compared across 1 parsed release.
About this page
Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.