setup_postdata() WordPress Function

The setup_postdata() function is used to initialize the global $post variable with data from a specified post. This function is typically used within The Loop.

setup_postdata( WP_Post|object|int $post ) #

Set up global post data.


Parameters

$post

(WP_Post|object|int)(Required)WP_Post instance or Post ID/object.


Top ↑

Return

(bool) True when finished.


Top ↑

More Information

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.

Top ↑

Usage


<?php 
global $post;

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

setup_postdata( $post ); 
?>

 

 


Top ↑

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


Top ↑

Changelog

Changelog
VersionDescription
4.4.0Added the ability to pass a post ID to $post.
1.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.