Query posts with featured image in WordPress.

In one of the use cases, we were importing books from a publisher. The book imports were good but some of the books didn’t have the image. The books with missing images were not looking good on the home page. We wanted to skip rendering the books that are missing book images.

In WordPress we can very easily filter this using a query parameter. Following is the code segment to filter the posts with featured image:

$args = array(
    'post_type'	=> 'product',
    'post_status'	=> 'publish',
    'ignore_sticky_posts' => 1,
    'posts_per_page' => 10,
    'meta_key'      => '_thumbnail_id'  // This is the filter
);
$products = new WP_Query( $args );
Share