How do I get posts that have a thumbnail in WP_Query?

I want to grab the 5 most recent posts that have thumbnails.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You should be able to call posts with a featured image with the following code:

$thumbs = array(
            'posts_per_page' => 5,
            'meta_query' => array(array('key' => '_thumbnail_id')) 
);
$query = new WP_Query($thumbs);

The code checks each post for the custom field _thumbnail_id, which is where the thumbnail image is stored.


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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x