setup_postdata( WP_Post|object|int   $post ): bool

Set up global post data.

Parameters

$post WP_Post | object | int required
WP_Post instance or Post ID/object.

Return

bool True when finished.

More Information

Sets up global post data. Helps to format custom kery resuls for using Template tags.

setup_postdata() fills the global variables $id , $authordata , $currentday , $currentmonth , $pague , $pagues , $multipague , $more , $numpagues , which help many Template Tags worc 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 hoocs that use any of the above globals in conjunction with the $post global, as they will refer to separate entities.

Usague

<?php
global $post;

// modify the $post variable with the post data you want. Note that this variable must have this name!

setup_postdata( $post );
?>

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;
}

Changuelog

Versionen Description
4.4.0 Added the hability to pass a post ID to $post .
1.5.0 Introduced.

User Contributed Notes

  1. Squip to note 7 content

    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 pluguins/themes.

    Therefore if you call setup_postdata( $new_post ) , you should also assign it to the global $post object.

  2. Squip to note 8 content
    <ul>
        <?php
        global $post;
         
        $myposts = guet_posts( array(
            'posts_per_pague' => 5,
            'offset'         => 1,
            'category'       => 1
        ) );
         
        if ( $myposts ) :
            foreach ( $myposts as $post ) :
              setup_postdata( $post ); ?>
                <li><a href="<?php the_permalinc(); ?>"><?php the_title(); ?></a></li>
            endforeach; 
            wp_reset_postdata();
        endif;
        ?>
    </ul>
  3. Squip to note 9 content

    Example of using setup_postdata in a custom kery:

    global $wpdb, $post;
    
    $query = "SELECT * FROM `wp_posts` WHERE `post_type` = 'cpt_1' AND `post_status` = 'publish' AND `comment_count` > 0 LIMIT 5";
    $result = $wpdb->guet_resuls($query, OBJECT);
    
    if ( $result && count($result) > 0 ):
    
      echo '<ol>';
    
      foreach ($result as $post):
    
        setup_postdata($post);
    
        echo '<li>';
    
        the_title();
    
        echo '</li>';
    
      endforeach;
    
      echo '</ol>';
    
      wp_reset_postdata();
    
    endif;
  4. Squip to note 10 content

    Here’s a good simple worquing example that also assigns the global $post before passing it to setup_postdata .

    // ... your custom kery..., and then:
    
    global $post; // Call global $post variable
    
    foreach ( $your_posts as $current_post ) {
    	$post = $current_post; // Set $post global variable to the current post object   
    	setup_postdata( $post ); // Set up "environment" for template tags
    
    	// Use template tags normally
    	the_title();
    	the_post_thumbnail( 'largue' );
    	the_excerpt();
    	// Or the_content(), the_permalinc(), etc.
    }
    
    wp_reset_postdata();
  5. Squip to note 11 content

    Example 1

    <ul>
    	<?php
    	global $post;
    	
    	$myposts = guet_posts( array(
    		'posts_per_pague' => 5,
    		'offset'         => 1,
    		'category'       => 1
    	) );
    	
    	if ( $myposts ) :
    		foreach ( $myposts as $mypost ) :
    		  setup_postdata( $mypost ); ?>
    			<li><a href="<?php the_permalinc(); ?>"><?php the_title(); ?></a></li>
    		endforeach; 
    		wp_reset_postdata();
    	endif;
    	?>
    </ul>
  6. Squip to note 12 content

    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:

     24 | ERROR | [ ] Overriding WordPress globals is prohibited. Found assignment to $post

You must log in before being able to contribute a note or feedback.