[wp-trac] [WordPress Trac] #50847: update_post_thumbnail_cache returns notice when get_the_post_thumbnail used with ID after setup_postdata()

WordPress Trac noreply at wordpress.org
Tue Aug 4 17:23:43 UTC 2020


#50847: update_post_thumbnail_cache returns notice when get_the_post_thumbnail used
with ID after setup_postdata()
--------------------------+-----------------------------
 Reporter:  tomcent       |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  5.4.2
 Severity:  minor         |   Keywords:  needs-patch
  Focuses:                |
--------------------------+-----------------------------
 **Situation:**
 - Inside main loop
 - foreach over array of (related) post_id's,
 - use ''setup_postdata($post)'' for each item
 - Items call get_template_part(something)
 - Template parts uses ''get_the_post_thumbnail'' which allow a post_ID
 - ''get_the_post_thumbnail'' calls ''update_post_thumbnail_cache''
 (because in_the_loop)
 - ''update_post_thumbnail_cache'' gives **notice on line 101: Trying to
 get property 'ID' of non-object**
 - Restore main loop with wp_reset_postdata()

 **Possible fix:**
 Change this
 {{{#!php
 <?php
 foreach ( $wp_query->posts as $post ) {
         $id = get_post_thumbnail_id( $post->ID );
         if ( $id ) {
                 $thumb_ids[] = $id;
         }
 }
 }}}
 To this
 {{{#!php
 <?php
 foreach ( $wp_query->posts as $post ) {
         if (is_object($post)) {
                 $post = $post->ID;
         }
         $id = get_post_thumbnail_id( $post );
         if ( $id ) {
                 $thumb_ids[] = $id;
         }
 }
 }}}
 in /wp-includes/post-thumbnail-template.php line 101

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


More information about the wp-trac mailing list