# Query Loop Block: Only Posts With a Featured Image

> Limits any Query Loop block with the class "wpide-has-thumbnail" to posts that have a featured image, so image grids never show empty cards.

- Type: PHP
- Runs: Run everywhere
- Category: [Block Editor](https://wpide.com/library/category/gutenberg/)
- Tags: query-loop, featured-image, grid
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/query-loop-with-featured-image/

## Code

```php
$wpide_query_thumbs = array( 'active' => false );

add_filter( 'pre_render_block', function ( $pre, $parsed ) use ( &$wpide_query_thumbs ) {
    if ( 'core/query' === $parsed['blockName'] ) {
        $classes = isset( $parsed['attrs']['className'] ) ? ' ' . $parsed['attrs']['className'] . ' ' : '';
        $wpide_query_thumbs['active'] = false !== strpos( $classes, ' wpide-has-thumbnail ' );
    }

    return $pre;
}, 10, 2 );

add_filter( 'query_loop_block_query_vars', function ( $query ) use ( &$wpide_query_thumbs ) {
    if ( ! $wpide_query_thumbs['active'] ) {
        return $query;
    }

    $meta_query   = isset( $query['meta_query'] ) ? (array) $query['meta_query'] : array();
    $meta_query[] = array( 'key' => '_thumbnail_id', 'compare' => 'EXISTS' );

    $query['meta_query'] = $meta_query;

    return $query;
} );
```

## Notes

Add wpide-has-thumbnail to the Query Loop's Additional CSS class(es).

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Query Loop Block: Only Posts With a Featured Image" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
