wppaste
WordPress

WP_Query::generate_postdata( WP_Post|object|int $post ): array|false

Since
5.2.0
Source
wp-includes/class-wp-query.php:4873
Generates post data.

Compatibility

WordPress
since 5.2.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$postWP_Post|object|int
WP_Post instance or Post ID/object.

Return value

array|false
Elements of post or false on failure.

Performance profile

How much work a call to WP_Query::generate_postdata() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
5–121

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 129.

Plugin surface
1 hook

Third-party callbacks on 'content_pagination' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 18 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Query::generate_postdata() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$post instanceof5none
!($post instanceof)9get_post()
$post instanceof && !$content66–89get_userdata(), ->get(), get_queried_object_id(), ->is_feed(), apply_filters(), compact()
$post instanceof && ->is_page() && !$content67–89get_userdata(), ->get(), get_queried_object_id(), ->is_page(), apply_filters(), compact()
$post instanceof && !->is_page() && ->is_single() && !$content70–92get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), apply_filters(), compact()
!($post instanceof) && !$content70–93get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_feed(), apply_filters(), compact()
!($post instanceof) && ->is_page() && !$content71–93get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_page(), apply_filters(), compact()
$post instanceof && !->is_page() && !->is_single() && !$content72–95get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), ->is_feed(), apply_filters(), compact()
!($post instanceof) && !->is_page() && ->is_single() && !$content74–96get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), apply_filters(), compact()
!($post instanceof) && !->is_page() && !->is_single() && !$content76–99get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), ->is_feed(), apply_filters(), compact()
$post instanceof && $content86–111get_userdata(), ->get(), get_queried_object_id(), ->is_feed(), explode(), apply_filters(), compact()
$post instanceof && ->is_page() && $content87–111get_userdata(), ->get(), get_queried_object_id(), ->is_page(), explode(), apply_filters(), compact()
6 further outcomes, up to 121 instructions
$post instanceof && !->is_page() && ->is_single() && $content90–114get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), explode(), apply_filters(), compact()
!($post instanceof) && $content90–115get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_feed(), explode(), apply_filters(), compact()
!($post instanceof) && ->is_page() && $content91–115get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_page(), explode(), apply_filters(), compact()
$post instanceof && !->is_page() && !->is_single() && $content92–117get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), ->is_feed(), explode(), apply_filters(), compact()
!($post instanceof) && !->is_page() && ->is_single() && $content94–118get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), explode(), apply_filters(), compact()
!($post instanceof) && !->is_page() && !->is_single() && $content96–121get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), ->is_feed(), explode(), apply_filters(), compact()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1295–12113
8.51295–12113
8.41295–1211334 fewer instructions than PHP 8.3
8.31635–15513
8.21635–15513
8.11635–15513
7.41635–15513

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while WP_Query::generate_postdata() runs, in this order:

  1. apply_filters( content_pagination )filterline 4950 (+77 into the body)

    Filters the "pages" derived from splitting the post content.

Uses · 10

Used by · 1

Source code

	public function generate_postdata( $post ) { 		if ( ! ( $post instanceof WP_Post ) ) {			$post = get_post( $post );		} 		if ( ! $post ) {			return false;		} 		$id = (int) $post->ID; 		$authordata = get_userdata( $post->post_author ); 		$currentday   = false;		$currentmonth = false; 		$post_date = $post->post_date;		if ( ! empty( $post_date ) && '0000-00-00 00:00:00' !== $post_date ) {			// Avoid using mysql2date for performance reasons.			$currentmonth = substr( $post_date, 5, 2 );			$day          = substr( $post_date, 8, 2 );			$year         = substr( $post_date, 2, 2 ); 			$currentday = sprintf( '%s.%s.%s', $day, $currentmonth, $year );		} 		$numpages  = 1;		$multipage = 0;		$page      = $this->get( 'page' );		if ( ! $page ) {			$page = 1;		} 		/*		 * Force full post content when viewing the permalink for the $post,		 * or when on an RSS feed. Otherwise respect the 'more' tag.		 */		if ( get_queried_object_id() === $post->ID && ( $this->is_page() || $this->is_single() ) ) {			$more = 1;		} elseif ( $this->is_feed() ) {			$more = 1;		} else {			$more = 0;		} 		$content = $post->post_content;		if ( str_contains( $content, '<!--nextpage-->' ) ) {			$content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );			$content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );			$content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content ); 			// Remove the nextpage block delimiters, to avoid invalid block structures in the split content.			$content = str_replace( '<!-- wp:nextpage -->', '', $content );			$content = str_replace( '<!-- /wp:nextpage -->', '', $content ); 			// Ignore nextpage at the beginning of the content.			if ( str_starts_with( $content, '<!--nextpage-->' ) ) {				$content = substr( $content, 15 );			} 			$pages = explode( '<!--nextpage-->', $content );		} else {			$pages = array( $post->post_content );		} 		/**		 * Filters the "pages" derived from splitting the post content.		 *		 * "Pages" are determined by splitting the post content based on the presence		 * of `<!-- nextpage -->` tags.		 *		 * @since 4.4.0		 *		 * @param string[] $pages Array of "pages" from the post content split by `<!-- nextpage -->` tags.		 * @param WP_Post  $post  Current post object.		 */		$pages = apply_filters( 'content_pagination', $pages, $post ); 		$numpages = count( $pages );

Changelog

Introduced in 5.2.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/class-wp-query.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.