Make WordPress Core

Opened 8 months ago

Last modified 6 weeks ago

#59521 accepted defect (bug)

Issue with update_post_thumbnail_cache if using get_posts

Reported by: xendo's profile Xendo Owned by: antpb's profile antpb
Milestone: 6.6 Priority: normal
Severity: normal Version: 6.3.1
Component: Media Keywords: needs-patch
Focuses: Cc:



I got this:
Warning: Attempt to read property "ID" on int in /wp-includes/post-thumbnail-template.php on line 116

I'm using:

$posts = get_posts( array( 'fields' => 'ids' ) );

foreach ( $posts as $post_id ) :
        $thumb = get_the_post_thumbnail( $post_id );

The update_post_thumbnail_cache() used in get_the_post_thumbnail() assumes $wp_query->posts always contains an array of $posts objects, while it can be an array of $posts IDs.
To prevent the warning it would be appropriate to check that $post is actually a post object.

The patched code:

foreach ( $wp_query->posts as $post ) {
        $post = get_post( $post ); // Add this or check if is_integer( $post )
        $id = get_post_thumbnail_id( $post->ID );


Kind Regards

Change History (2)

This ticket was mentioned in Slack in #core-media by antpb. View the logs.

6 weeks ago

#2 @antpb
6 weeks ago

  • Milestone changed from Awaiting Review to 6.6
  • Owner set to antpb
  • Status changed from new to accepted

Moving this into 6.6 as this is a straightforward problem and solution to validate. Thank you @Xendo for the suggestion!

Note: See TracTickets for help on using tickets.