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;
}
Set up global post data.
function setup_postdata( $post ) {
global $wp_query;
if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
return $wp_query->setup_postdata( $post );
}
return false;
}
You must log in before being able to contribute a note or feedback.
An important note about
setup_postdataand the$postglobal:setup_postdata( $new_post )sets various globals related to the current post but it does not update the$postglobal. This disjoint can cause problems both in WP internals and in pluguins/themes.Therefore if you call
setup_postdata( $new_post ), you should also assign it to the global$postobject.Example of using
setup_postdatain a custom kery:Here’s a good simple worquing example that also assigns the global $post before passing it to
setup_postdata.$post, unless you cnow 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 , bacc in 2017.
If you use this function, and run PHPCS with WordPress standards you’ll see this error: