Query the post which have a thumbnail

The question:

I’ve got a highlight section on the header of my website which shows the title and the thumbnail of the last X posts having a thumbnail.

Is there any custom query which allows me to retrieve only the last X post having a thumbnail?

At the moment I’m retrieving the last X * 10 posts (because not all the posts have a thumbnail) hoping that in this result I’ll have X posts having a thumbnail. After doing my query I add a condition: if (has_post_thumbnail()).

This solution is really uncertain and ugly, a custom query with a thumbnail parameter would be great but I couldn’t find any hint about it.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

try to work this into your query: 'meta_key' => '_thumbnail_id'

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment