Title: update_post_thumbnail_cache
Published: April 25, 2014
Last modified: February 24, 2026

---

# update_post_thumbnail_cache( WP_Query|null $wp_query = null )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/update_post_thumbnail_cache/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/functions/update_post_thumbnail_cache/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/update_post_thumbnail_cache/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/update_post_thumbnail_cache/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/update_post_thumbnail_cache/?output_format=md#wp--skip-link--target)

Updates cache for thumbnails in the current loop.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/update_post_thumbnail_cache/?output_format=md#parameters)󠁿

 `$wp_query`[WP_Query](https://developer.wordpress.org/reference/classes/wp_query/)
|nulloptional

A [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/) instance.
Defaults to the $wp_query global.

Default:`null`

## 󠀁[Source](https://developer.wordpress.org/reference/functions/update_post_thumbnail_cache/?output_format=md#source)󠁿

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

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post-thumbnail-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/post-thumbnail-template.php#L104)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/post-thumbnail-template.php#L104-L144)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/update_post_thumbnail_cache/?output_format=md#related)󠁿

| Uses | Description | 
| [get_post_thumbnail_id()](https://developer.wordpress.org/reference/functions/get_post_thumbnail_id/)`wp-includes/post-thumbnail-template.php` |

Retrieves the post thumbnail ID.

  | 
| [_prime_post_caches()](https://developer.wordpress.org/reference/functions/_prime_post_caches/)`wp-includes/post.php` |

Adds any posts from the given IDs to the cache that do not already exist in cache.

  |

| Used by | Description | 
| [WP_REST_Posts_Controller::get_items()](https://developer.wordpress.org/reference/classes/wp_rest_posts_controller/get_items/)`wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php` |

Retrieves a collection of posts.

  | 
| [WP_Media_List_Table::prepare_items()](https://developer.wordpress.org/reference/classes/wp_media_list_table/prepare_items/)`wp-admin/includes/class-wp-media-list-table.php` |  | 
| [get_the_post_thumbnail()](https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/)`wp-includes/post-thumbnail-template.php` |

Retrieves the post thumbnail.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/update_post_thumbnail_cache/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.2.0](https://developer.wordpress.org/reference/since/3.2.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fupdate_post_thumbnail_cache%2F)
before being able to contribute a note or feedback.