WordPress Query: Exclude Posts With No Featured Image

So – I make WordPress themes – lots of them 🙂

Something that’s bugged me for a while is that I can’t easily use the WP_Query to select posts with featured images. This isn’t functionality that’s needed that often – but it can be desirable for things like image carousels where you need an image to make everything look good.

Today however – I finally found a solution – and it’s surprisingly simple. All post data – including the featured image url – us stored in WordPress post meta, it’s a shame I hadn’t made the connection before. All I have to do to select posts that use featured images is check for the existence of a meta property that is typically hidden.

To do this the query would look something like this:

$args = array( 'meta_key' => '_thumbnail_id' );
$query = new WP_Query( $args );

For more information on the WP_Query I wrote this post with some WP_Query features you might not know.

How was it for you? Let me know on BlueSky or Mastodon

(Please) Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

Related Posts

14 May 2013

Redesigning the WordPress Post Editor

Ghost is a project born from frustration with WordPress. Ironically it seems to be mostly WordPress power users who want to use it. The Ghost team – led by John O’Nolan – put Ghost on KickStarter last week and it...
01 Jul 2014

I No Longer Use TimThumb – Here’s What I do Instead

Last week there was a second exploit found in TimThumb. Thankfully it was no-where near as bad as the first one – but it raised an interesting question of whether TimThumb is even needed anymore.TimThumb was made to be useful...
01 Apr 2015

The State of WordPress Themes #wcldn

I recently spoke on a panel at WordCamp London 2015e. Lance – who used to be the Theme Team lead at WordPress.com – asked me if I wanted to speak on a panel with him at WordCamp London 2015. I’ve...
30 Mar 2010

10 WordPress query_posts tips you probably don’t know

I have written a really brief query_post tutorial before, and it did quite well, but both WordPress and my own skills, have advanced considerably since then. So I thought it would be interesting to revisit the query_posts command and see...
13 May 2010

6 Tips to Build Better WordPress Themes

If you want to make WordPress themes, for clients, to release for free or to sell, then there are a lot of factors you need to take into consideration. Below are some hints and tips that should help ease your...
08 Aug 2013

The Death of WordPress Theme Frameworks

WordPress theme frameworks are on their way out. They’re dying a slow death. At least that’s what I think.Nathan Rice recently wrote an article with his thoughts about theme frameworks – in defense of them – however he clearly has...