[wp-trac] [WordPress Trac] #59521: Issue with update_post_thumbnail_cache if using get_posts

WordPress Trac noreply at wordpress.org
Tue Oct 3 09:25:40 UTC 2023


#59521: Issue with update_post_thumbnail_cache if using get_posts
--------------------------+-----------------------------
 Reporter:  Xendo         |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Media         |    Version:  6.3.1
 Severity:  normal        |   Keywords:  needs-patch
  Focuses:                |
--------------------------+-----------------------------
 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
 <?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
 <?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

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/59521>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list