Query posts only with featured image

When running the importer, you must check the checkbox to download and import all media/attachments.

If you do not, the posts will still have a featured image set, but the attachment they refer to will be invalid, and any attempt to call the_post_thumbnail will fail. Using your code you would get just the post title, and no thumbnail. This would give the impression that posts that have no featured image are being pulled in. This is not the case, they have a featured image, it’s just not available because of the mistake you made when importing.

Delete your posts, and re-import using the correct settings.

Also to demonstrate the point, you made no attempt to actually check the posts, always check e.g.:

if ( $query->have_posts() ) { // you never checked to see if no posts were found
    while($query->have_posts()) { // alt style syntax doesn't work with most IDEs
        $query->the_post(); // individual statement should be on individual line
        ?><h2><?php the_title(); ?></h2><?php // you only need open/close tags here, not every line, save yourself some time typing
        if ( has_post_thumbnail() ) { // only print out the thumbnail if it actually has one
            echo '<p>post says it has a featured image</p>'; // double checking
            the_post_thumbnail('thumbnail');
        } else {
            echo '<p>this post does not have a featured image</p>';
        }
    }
} else {
    echo '<p>no posts found</p>';
}

If you have not checked the checkbox, you will get images saying they have a featured image, but no image is shown

Leave a Comment