WP_Query::generate_postdata( WP_Post|object|int $post ): array|false
- Since
- 5.2.0
- Source
wp-includes/class-wp-query.php:4768
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
- Scaling
- Constant
- Instructions
- 5–121
- Plugin surface
- 1 hook
- Called by
- 1
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 129.
Third-party callbacks on 'content_pagination' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
$post instanceof | 5 | none |
!($post instanceof) | 9 | get_post() |
$post instanceof && !$content | 66–89 | get_userdata(), ->get(), get_queried_object_id(), ->is_feed(), apply_filters(), compact() |
$post instanceof && ->is_page() && !$content | 67–89 | get_userdata(), ->get(), get_queried_object_id(), ->is_page(), apply_filters(), compact() |
$post instanceof && !->is_page() && ->is_single() && !$content | 70–92 | get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), apply_filters(), compact() |
!($post instanceof) && !$content | 70–93 | get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_feed(), apply_filters(), compact() |
!($post instanceof) && ->is_page() && !$content | 71–93 | get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_page(), apply_filters(), compact() |
$post instanceof && !->is_page() && !->is_single() && !$content | 72–95 | get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), ->is_feed(), apply_filters(), compact() |
!($post instanceof) && !->is_page() && ->is_single() && !$content | 74–96 | get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), apply_filters(), compact() |
!($post instanceof) && !->is_page() && !->is_single() && !$content | 76–99 | get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), ->is_feed(), apply_filters(), compact() |
$post instanceof && $content | 86–111 | get_userdata(), ->get(), get_queried_object_id(), ->is_feed(), explode(), apply_filters(), compact() |
$post instanceof && ->is_page() && $content | 87–111 | get_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() && $content | 90–114 | get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), explode(), apply_filters(), compact() |
!($post instanceof) && $content | 90–115 | get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_feed(), explode(), apply_filters(), compact() |
!($post instanceof) && ->is_page() && $content | 91–115 | get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_page(), explode(), apply_filters(), compact() |
$post instanceof && !->is_page() && !->is_single() && $content | 92–117 | get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), ->is_feed(), explode(), apply_filters(), compact() |
!($post instanceof) && !->is_page() && ->is_single() && $content | 94–118 | get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), explode(), apply_filters(), compact() |
!($post instanceof) && !->is_page() && !->is_single() && $content | 96–121 | get_post(), get_userdata(), ->get(), get_queried_object_id(), ->is_page(), ->is_single(), ->is_feed(), explode(), apply_filters(), compact() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 129 | 5–121 | 13 | |
| 8.5 | 129 | 5–121 | 13 | |
| 8.4 | 129 | 5–121 | 13 | 34 fewer instructions than PHP 8.3 |
| 8.3 | 163 | 5–155 | 13 | |
| 8.2 | 163 | 5–155 | 13 | |
| 8.1 | 163 | 5–155 | 13 | |
| 7.4 | 163 | 5–155 | 13 |
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:
- apply_filters( content_pagination )filterline 4845 (+77 into the body)
Filters the "pages" derived from splitting the post content.
Uses · 10
- get_post()Retrieves post data given a post ID or post object.
- get_userdata()Retrieves user info by user ID.
- get_queried_object_id()Retrieves the ID of the currently queried object.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_Query::get()Retrieves the value of a query variable.
- WP_Query::is_page()Determines whether the query is for an existing single page.
- WP_Query::is_single()Determines whether the query is for an existing single post.
- WP_Query::is_feed()Determines whether the query is for a feed.
Used by · 1
- WP_Query::setup_postdata()Sets up global post data.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.