Use WP CLI to find all blog posts without a featured image - two methods


This uses the wp shell command. It gives you an interactive prompt into which you can do various WordPress "things".

One small annoyance is that it doesn't like multi-line entry. It treats every hit of the enter key as "plz run the codez" - so, at the end of this blog post, I've put the commands in copy-n-paste format.

Once you've installed WP CLIP, go to the command line and run wp shell. You'll be greeted with an interactive prompt wp>

This command constructs a query which gets all posts, which have been published, where there is no thumbnail ID. Note - this isn't quite the same as not having a featured image.

PHP PHP$args = array(
    'post_type'     => 'post',
    'post_status'   => array('publish'),
    'posts_per_page'=> -1,
    'meta_query'    => array(
        array(
            'key' => '_thumbnail_id',
            'compare' => 'NOT EXISTS'
        )
    ),
);

Next we run that query. It will dump quite a lot of information into the screen, we'll format it shortly.

PHP PHP$query = new WP_Query( $args );

Finally, we loop through all the posts the query has found and print them out in a nice format.

PHP PHP$posts = $query->posts;
foreach ( $posts as $post ) {
    echo $post->post_date . " " . $post->guid . " " . $post->post_title . "\n";
}

That will give you the date, link, and title of every post where there is no featured image set.

Copy and Paste Snippet

PHP PHP$args = array( 'post_type' => 'post', 'post_status' => array('publish'), 'posts_per_page'=> -1,  'meta_query' => array(       array( 'key' => '_thumbnail_id', 'value' => '?', 'compare' => 'NOT EXISTS' ) ),);
$query = new WP_Query( $args );
$posts = $query->posts;
foreach ( $posts as $post ) { echo $post->post_date . " " . $post->guid . " " . $post->post_title . "\n"; }

Sometimes the WordPress database can get a little confused. It will say there is a post thumbnail, but when you try to retrieve it, there's nothing there!

This method is slightly slower if you have lots of posts. It goes through every single post and checks whether the featured image can be retrieved. If not it will let you know.

PHP PHP$args = array(
    'post_type'     => 'post',
    'post_status'   => array('publish'),
    'posts_per_page'=> -1,
);

$posts = get_posts( $args );

foreach ( $posts => $post) {
    if( get_the_post_thumbnail( $post ) == "" ) {
        echo $post->post_date . " " . $post->guid . " " . $post->post_title . "\n"; }
}

Copy and Paste Snippet

PHP PHPforeach (get_posts( array( 'post_type' => 'post', 'post_status' => array('publish'), 'posts_per_page' => -1,) ) as $post) { if(get_the_post_thumbnail($post)== "") { echo $post->post_date . " " . $post->guid . " " . $post->post_title . "\n"; } }

Share this post on…

  • Mastodon
  • Facebook
  • LinkedIn
  • BlueSky
  • Threads
  • Reddit
  • HackerNews
  • Lobsters
  • WhatsApp
  • Telegram

What are your reckons?

All comments are moderated and may not be published immediately. Your email address will not be published.

Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <p> <pre> <br> <img src="" alt="" title="" srcset="">