Filter Posts With or Without a Featured Image
Adds a "Featured image" dropdown above admin post lists to show only posts that have one or only those that are missing one, which makes fixing gaps quick.
Opens WPIDE on
Code
add_action( 'restrict_manage_posts', function ( $post_type ) {
if ( ! post_type_supports( $post_type, 'thumbnail' ) ) {
return;
}
$current = isset( $_GET['wpide_thumb'] ) ? sanitize_key( $_GET['wpide_thumb'] ) : '';
?>
<label class="screen-reader-text" for="wpide-thumb-filter">Filter by featured image</label>
<select name="wpide_thumb" id="wpide-thumb-filter">
<option value="">Featured image: any</option>
<option value="with" <?php selected( $current, 'with' ); ?>>With featured image</option>
<option value="without" <?php selected( $current, 'without' ); ?>>Without featured image</option>
</select>
<?php
} );
add_action( 'pre_get_posts', function ( $query ) {
if ( ! is_admin() || ! $query->is_main_query() || empty( $_GET['wpide_thumb'] ) ) {
return;
}
$compare = 'with' === $_GET['wpide_thumb'] ? 'EXISTS' : 'NOT EXISTS';
$meta = is_array( $query->get( 'meta_query' ) ) ? $query->get( 'meta_query' ) : array();
$meta[] = array( 'key' => '_thumbnail_id', 'compare' => $compare );
$query->set( 'meta_query', $meta );
} );
Notes
Works on every post type that supports featured images.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.