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.