wppaste
WordPress

update_post_thumbnail_cache( WP_Query|null $wp_query = null )

Since
3.2.0
Source
wp-includes/post-thumbnail-template.php:104
Updates cache for thumbnails in the current loop.

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

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
5–31

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
7

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

What it touches

  • querycontent queryget_post()one call below update_post_thumbnail_cache()
  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
$wp_query5–7none
!$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

PHPCompiledExecutedBranchesNotes
8.6-dev515–3110
8.5515–3110
8.4515–3110
8.3515–3110
8.2515–3110
8.1515–31101 fewer instruction than PHP 7.4
7.4525–3210

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

Used by · 7

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.

  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.

6.8.8
Parameter $wp_query retyped from WP_Query to WP_Query|null.verified against source
3.2.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.