WordPress.org

Make WordPress Core

Opened 4 months ago

Last modified 3 months ago

#50847 new defect (bug)

update_post_thumbnail_cache returns notice when get_the_post_thumbnail used with ID after setup_postdata()

Reported by: tomcent Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.4.2
Component: Post Thumbnails Keywords: needs-patch
Focuses: Cc:

Description

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
foreach ( $wp_query->posts as $post ) {
        $id = get_post_thumbnail_id( $post->ID );
        if ( $id ) {
                $thumb_ids[] = $id;
        }
}

To this

<?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

Change History (2)

#1 @SergeyBiryukov
4 months ago

  • Component changed from General to Post Thumbnails

#2 in reply to: ↑ description @tomcent
3 months ago

  • Severity changed from minor to normal

Replying to tomcent:

update_post_thumbnail_cache gives notice on line 101: Trying to get property 'ID' of non-object

Change in /wp-includes/post-thumbnail-template.php line 101

This is now line 106 after the WP 5.5 update.
The issue is still present on this version.

Note: See TracTickets for help on using tickets.