update_post_thumbnail_cache( WP_Query|null $wp_query = null )
- Since
- 3.2.0
- Source
wp-includes/post-thumbnail-template.php:104
Compatibility
- WordPress
- since 3.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
$wp_queryWP_Query|nulloptional- A WP_Query instance. Defaults to the $wp_query global.Default:
null
Performance profile
How much work a call to update_post_thumbnail_cache() 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
- Scales with input
- Instructions
- 5–31
- Plugin surface
- None
- Called by
- 7
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 51.
Nothing here hands control to plugin code.
7 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()one call below update_post_thumbnail_cache() - hookthird-party callbacks
apply_filters()one call below update_post_thumbnail_cache()
Further down the call graph this can also reach cache, serialize, option 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 · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost update_post_thumbnail_cache() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$wp_query | 5–7 | none |
!$wp_query && empty($thumb_ids) | 22–26 | _prime_post_caches() |
!$wp_query && !empty($thumb_ids) | 27–31 | _prime_post_caches(), _prime_post_caches() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 51 | 5–31 | 10 | |
| 8.5 | 51 | 5–31 | 10 | |
| 8.4 | 51 | 5–31 | 10 | |
| 8.3 | 51 | 5–31 | 10 | |
| 8.2 | 51 | 5–31 | 10 | |
| 8.1 | 51 | 5–31 | 10 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 52 | 5–32 | 10 |
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.
Uses · 2
- _prime_post_caches()Adds any posts from the given IDs to the cache that do not already exist in cache.
- get_post_thumbnail_id()Retrieves the post thumbnail ID.
Used by · 7
- WP_Media_List_Table::prepare_items()Prepares the list of items for displaying.
- WP_REST_Posts_Controller::get_items()Retrieves a collection of posts.
- get_the_post_thumbnail()Retrieves the post thumbnail.
- render_block_core_cover()Renders the `core/cover` block on server.
- render_block_core_latest_posts()Renders the `core/latest-posts` block on server.
- render_block_core_media_text()Renders the `core/media-text` block on server.
- render_block_core_post_template()Renders the `core/post-template` block on the server.
Source code
function update_post_thumbnail_cache( $wp_query = null ) { if ( ! $wp_query ) { $wp_query = $GLOBALS['wp_query']; } if ( $wp_query->thumbnails_cached ) { return; } $thumb_ids = array(); /* * $wp_query may contain an array of post objects or post IDs. * * This ensures the cache is primed for all post objects to avoid * `get_post()` calls in `get_the_post_thumbnail()` triggering an * additional database call for each post. */ $parent_post_ids = array(); foreach ( $wp_query->posts as $post ) { if ( $post instanceof WP_Post ) { $parent_post_ids[] = $post->ID; } elseif ( is_int( $post ) ) { $parent_post_ids[] = $post; } } _prime_post_caches( $parent_post_ids, false, true ); foreach ( $wp_query->posts as $post ) { $id = get_post_thumbnail_id( $post ); if ( $id ) { $thumb_ids[] = $id; } } if ( ! empty( $thumb_ids ) ) { _prime_post_caches( $thumb_ids, false, true ); } $wp_query->thumbnails_cached = true;}Changelog
Introduced in 3.2.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$wp_query retyped from WP_Query to WP_Query|null.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/post-thumbnail-template.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.