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.

Was it good/ useful/ a load of old rubbish? Let me know on Mastodon, or BlueSky (or Twitter X if you must).

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.

WordPress News

The latest WordPress updates from the WPBriefs Podcast.

Related Posts

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...
28 May 2010

WordPress caching, Part 2

As I mentioned in WordPress caching part 1, WordPress has built-in caching that can be hooked into by plugins such as W3 Total Cache and Batcache (developed by Andy Skelton who is employed by Automattic).In this article I am going...
19 Apr 2011

Simple WordPress Post Thumbnails with Regular Expressions

These days everyone with a blog understands the importance of thumbnail images. Three years ago it was common place to have exclusively text, but now a nice image is a requirement. As such the process of implementing the image needs...
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...
04 Nov 2006

WordPress Tips and Tricks – Post in Advance

I’m sure most people know about this but the ability to post things in advance with WordPress is absolutely great. My two blogs (this one and Binary Joy) posted 16 articles whilst I was on holiday despite me not being...
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...