Make WordPress Core

Opened 10 months ago

Last modified 5 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.7 Priority: normal
Severity: normal Version: 6.3.1
Component: Media Keywords: has-patch has-unit-tests needs-testing
Focuses: Cc:

Description

Hello,

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

I'm using:

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

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

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:

<?php
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 );

Thanks

Kind Regards

Change History (8)

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


3 months ago

#2 @antpb
3 months 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!

This ticket was mentioned in PR #6636 on WordPress/wordpress-develop by @khokansardar.


2 months ago
#3

  • Keywords has-patch added; needs-patch removed

Fix attempt to read post ID on update_post_thumbnail_cache function as some cases $wp_query->posts return array of post IDs instead of WP_Post object.

Trac ticket: #59521

#4 @khokansardar
2 months ago

  • Keywords has-unit-tests added

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


8 weeks ago

This ticket was mentioned in Slack in #core by nhrrob. View the logs.


6 weeks ago

#7 @sumitsingh
6 weeks ago

  • Keywords needs-testing added

#8 @oglekler
5 weeks ago

  • Milestone changed from 6.6 to 6.7

It looks like straight forward solution but from my point of view there is still something strange in the functions code itself.

get_the_post_thumbnail() function calls update_post_thumbnail_cache() like this:

if ( in_the_loop() ) {
	update_post_thumbnail_cache();
}

and get_posts() should not set the loop 🤔 and if are getting posts in the loop that has nothing to do with the current post something needs to be done.

Btw, requesting posts one by one that were originally requested only with IDs is incorrect from the beginning because it's raising the amount of requests to the db. Query Monitor is simple tool to check if such things are happening in the code ‎😅 This worth checking from time to time.

We have RC1 today, so, I am moving this ticket to the next milestone for further work.

Note: See TracTickets for help on using tickets.