Within the loop, for a post (of custom post type) which does not have a featured image:

  1. has_post_thumbnail() (with or without specified $post->ID) is returning true, while
  2. get_the_post_thumbnail(), also with our without the $post->ID returns an empty string.

The loop:

$args = array(
'post_type' => 'customs', 
'post_status' => 'publish',
'fields' => 'id',
'posts_per_page' => -1
);
$query = new WP_Query($args);
while ( $query->have_posts() ) : $query->the_post(); 
  has_post_thumbnail();
  the_title(); // as expected
  the_excerpt(); // as expected
endwhile;

The CPT is defined as follows:

    $name          = 'Custom';
    $singular_name = 'Custom';
    $slug          = 'custom';
    $menu_icon     = 'universal-access';

    // Declare args and apply filters
    $args = array(
        'labels' => array(
            'name' => $name,
            'singular_name' => $singular_name,
            'add_new' => __( 'Add New', 'total' ),
            'add_new_item' => __( 'Add New Item', 'total' ),
            'edit_item' => __( 'Edit Item', 'total' ),
            'new_item' => __( 'Add New Custom Item', 'total' ),
            'view_item' => __( 'View Item', 'total' ),
            'search_items' => __( 'Search Items', 'total' ),
            'not_found' => __( 'No Items Found', 'total' ),
            'not_found_in_trash' => __( 'No Items Found In Trash', 'total' )
        ),
        'public' => true,
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'thumbnail',
            // 'comments',
            'custom-fields',
            'revisions',
            'author',
            'page-attributes',
        ),
        'capability_type' => 'post',
        'rewrite' => array( 'slug' => $slug, 'with_front' => false ),
        'has_archive' => true,
        'menu_icon' => 'dashicons-'. $menu_icon,
        'menu_position' => 20,
    );

    // Register the post type
    register_post_type( 'customs', $args );

}

While I can get desired outcome with:

if (get_the_post_thumbnail() !== ''):
  // display the thumbnail
else:
  // some other option
endif;

I’m confident that’s not the solution. Where else can I poke around for what’s causing this unexpected and undesired result?

Read more here: False Positive on has_post_thumbnail


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: