Set up global post data.
Parameters
Source
function setup_postdata( $post ) {
global $wp_query;
if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
return $wp_query->setup_postdata( $post );
}
return false;
}
CARVIEW |
Set up global post data.
Sets up global post data. Helps to format custom query results for using Template tags.
setup_postdata() fills the global variables $id
, $authordata
, $currentday
, $currentmonth
, $page
, $pages
, $multipage
, $more
, $numpages
, which help many Template Tags work in the current post context.
setup_postdata()
does not assign the global $post
variable so it’s important that you do this yourself. Failure to do so will cause problems with any hooks that use any of the above globals in conjunction with the $post
global, as they will refer to separate entities.
<?php
global $post;
// modify the $post variable with the post data you want. Note that this variable must have this name!
setup_postdata( $post );
?>
function setup_postdata( $post ) {
global $wp_query;
if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
return $wp_query->setup_postdata( $post );
}
return false;
}
Uses | Description |
---|---|
WP_Query::setup_postdata()wp-includes/class-wp-query.php | Sets up global post data. |
Used by | Description |
---|---|
WP_REST_Block_Renderer_Controller::get_item()wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php | Returns block output from block’s registered render_callback. |
WP_REST_Revisions_Controller::prepare_item_for_response()wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php | Prepares the revision for the REST response. |
WP_REST_Posts_Controller::prepare_item_for_response()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Prepares a single post output for response. |
wp_ajax_parse_embed()wp-admin/includes/ajax-actions.php | Applies Ajax handlers to a string. |
wp_ajax_parse_media_shortcode()wp-admin/includes/ajax-actions.php | |
WP_Posts_List_Table::single_row()wp-admin/includes/class-wp-posts-list-table.php | |
start_wp()wp-includes/deprecated.php | Sets up the WordPress Loop. |
You must log in before being able to contribute a note or feedback.
An important note about
setup_postdata
and the$post
global:setup_postdata( $new_post )
sets various globals related to the current post but it does not update the$post
global. This disjoint can cause problems both in WP internals and in plugins/themes.Therefore if you call
setup_postdata( $new_post )
, you should also assign it to the global$post
object.Example of using
setup_postdata
in a custom query:Here’s a good simple working example that also assigns the global $post before passing it to
setup_postdata
.$post
, unless you know you need to.Example 1
Note, it is probably best not to use this function, although it is neat, it goes against WordPress’ own coding standards.
Issue reported here, back in 2017.
If you use this function, and run PHPCS with WordPress standards you’ll see this error: