Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix attempt to read post ID on update_post_thumbnail_cache #6636

Closed
wants to merge 10 commits into from
19 changes: 18 additions & 1 deletion src/wp-includes/post-thumbnail-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,25 @@ function update_post_thumbnail_cache( $wp_query = null ) {

$thumb_ids = array();
peterwilsoncc marked this conversation as resolved.
Show resolved Hide resolved

/*
* $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->ID );
$id = get_post_thumbnail_id( $post );
if ( $id ) {
$thumb_ids[] = $id;
}
Expand Down
43 changes: 40 additions & 3 deletions tests/phpunit/tests/post/thumbnails.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,59 @@ public function test_get_post_thumbnail_id() {
$this->assertSame( self::$attachment_id, get_post_thumbnail_id() );
}

public function test_update_post_thumbnail_cache() {
/**
* Ensure `update_post_thumbnail_cache()` works when querying post objects.
*
* @ticket 59521
* @ticket 30017
* @ticket 33968
*
* @covers ::update_post_thumbnail_cache
*/
public function test_update_post_thumbnail_cache_when_querying_full_post_objects() {
set_post_thumbnail( self::$post, self::$attachment_id );

// Test case where `$query->posts` should return Array of post objects.
$query = new WP_Query(
array(
'post_type' => 'any',
'post__in' => array( self::$post->ID ),
'orderby' => 'post__in',
)
);

$this->assertFalse( $query->thumbnails_cached, 'Thumbnails should not be cached prior to calling update_post_thumbnail_cache().' );

update_post_thumbnail_cache( $query );

$this->assertTrue( $query->thumbnails_cached, 'Thumbnails should be cached after calling update_post_thumbnail_cache().' );
}

/**
* Ensure `update_post_thumbnail_cache()` works when querying post IDs.
*
* @ticket 59521
*
* @covers ::update_post_thumbnail_cache
*/
public function test_update_post_thumbnail_cache_when_querying_post_id_field() {
set_post_thumbnail( self::$post, self::$attachment_id );

// Test case where `$query2->posts` should return Array of post IDs.
$query = new WP_Query(
array(
'post_type' => 'any',
'post__in' => array( self::$post->ID ),
'orderby' => 'post__in',
'fields' => 'ids',
)
);

$this->assertFalse( $query->thumbnails_cached );
$this->assertFalse( $query->thumbnails_cached, 'Thumbnails should not be cached prior to calling update_post_thumbnail_cache().' );

update_post_thumbnail_cache( $query );

$this->assertTrue( $query->thumbnails_cached );
$this->assertTrue( $query->thumbnails_cached, 'Thumbnails should be cached after calling update_post_thumbnail_cache().' );
}

/**
Expand Down
Loading