setup_postdata( WP_Post|object|int $post )
Set up global post data.
Description Description
Parameters Parameters
Return Return
(bool) True when finished.
Source Source
File: wp-includes/query.php
function setup_postdata( $post ) {
global $wp_query;
if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
return $wp_query->setup_postdata( $post );
}
return false;
}
Expand full source code Collapse full source code View on Trac
Changelog Changelog
| Version | Description |
|---|---|
| 4.4.0 | Added the ability to pass a post ID to $post. |
| 1.5.0 | Introduced. |
User Contributed Notes User Contributed Notes
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 plugins/themes.Therefore if you call
setup_postdata( $new_post ), you should also assign it to the global$postobject.Example 2
<ul> <?php global $wpdb, $post; $str = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'"; $result = $wpdb->get_results( $str ); if ( $result ) { foreach ( $result as $post ): setup_postdata( $post ); ?> <li><a href="<?php the_permalink()?>"><?php the_title();?></a></li> <?php endforeach; } ?> </ul>Expand full source codeCollapse full source code
Example 1
Expand full source codeCollapse full source code