WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/setup postdata

Description

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.

Usage

<?php 
global $post;
setup_postdata$post ); 
?>

Parameters

$post
(object) (required) A post object.
Default: None

You must pass a reference to the global $post variable, otherwise functions like the_title() don't work properly.

For example:

   global $post;
   
   // Assign your post details to $post (& not any other variable name!!!!)
   $post = $post_object;
   
   setup_postdata( $post );
   // ...

A "clever" formulation:

   setup_postdata( $GLOBALS['post'] =& $post_object );

This will not work:

   global $post;
   
   setup_postdata( $post_object ); // oops

Return Values

This function always returns true.

Example 1

<ul>
<?php
global $post;
$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :
  setup_postdata($post); ?>
	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; 
wp_reset_postdata(); ?>
</ul>

Example 2

<ul>
<?php
global $wpdb,$post;
$result = $wpdb->get_results("SELECT $wpdb->posts.* FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'");
foreach($result as $post):
  setup_postdata($post);?>
	<li><a href="<?php the_permalink()?>"><?php the_title();?></a></li><?php 
endforeach;?>
</ul>

Change Log

Source File

setup_postdata() is located in wp-includes/query.php.

Related

Articles

Code Documentation

  • Class: WP_Query - Detailed Overview of class WP_Query
  • Class: WP_Comment_Query - Class for comment-related queries
  • Class: WP_User_Query - Class for user-related queries
  • Object: $wpdb - Overview on the use of the $wpdb object
  • Function: set_query_var()
  • Function: get_query_var()
  • Function: query_posts() - Create additional custom query
  • Function: get_post() - Take an ID of an item and return the records in the database for that article
  • Function: get_posts() - A specialized function that returns an array of items
  • Function: get_pages() - A specialized function that returns an array of pages
  • Function: have_posts() - A condition that determines whether the query returned an article
  • Function: the_post() - Used to automatically set the loop after a query
  • Function: rewind_posts() - Clears the current loop
  • Function: setup_postdata() - Sets the data for a single query result within a loop
  • Function: wp_reset_postdata() - Restores the previous query (usually after a loop within another loop)
  • Function: wp_reset_query()
  • Function: is_main_query() - Ensures that the query that is being changed is only the main query
  • Action Hook: pre_get_posts - Change WordPress queries before they are executed
  • Action Hook: the_post - Modify the post object after query
  • Filter Hook: found_posts - Changes the value of the object found_posts WP_Query
See also index of Function Reference and index of Template Tags.