Make WordPress Core


Ignore:
Timestamp:
10/14/2024 10:20:09 PM (19 months ago)
Author:
peterwilsoncc
Message:

Media: Account for post ID queries in update_post_thumbnail_cache().

Updates update_post_thumbnail_cache() to account for WP_Query objects that only contain the post ID field rather than the entire post object.

This changes passes the $post value to get_post_thumbnail_id() rather than assuming the presence of the ID property. Additionally, the posts to which the thumbnail is attached are now primed prior to calling the function to avoid numerous unnecessary database queries.

The test WP_Test_REST_Posts_Controller::test_get_items_primes_parent_post_caches() is modified to account for an order of operations change for the priming of post meta caches. The cache is no longer primed in the final call to update_meta_cache() so the tests need to account for the post meta to be primed in any call to the function.

Props antpb, jorbin, khokansardar, linsoftware, mukesh27, oglekler, rajinsharwar, sumitsingh, xendo.
Fixes #59521.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post-thumbnail-template.php

    r55821 r59235  
    113113    $thumb_ids = array();
    114114
     115    /*
     116     * $wp_query may contain an array of post objects or post IDs.
     117     *
     118     * This ensures the cache is primed for all post objects to avoid
     119     * `get_post()` calls in `get_the_post_thumbnail()` triggering an
     120     * additional database call for each post.
     121     */
     122    $parent_post_ids = array();
    115123    foreach ( $wp_query->posts as $post ) {
    116         $id = get_post_thumbnail_id( $post->ID );
     124        if ( $post instanceof WP_Post ) {
     125            $parent_post_ids[] = $post->ID;
     126        } elseif ( is_int( $post ) ) {
     127            $parent_post_ids[] = $post;
     128        }
     129    }
     130    _prime_post_caches( $parent_post_ids, false, true );
     131
     132    foreach ( $wp_query->posts as $post ) {
     133        $id = get_post_thumbnail_id( $post );
    117134        if ( $id ) {
    118135            $thumb_ids[] = $id;
Note: See TracChangeset for help on using the changeset viewer.