Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch all WordPress posts with featured image?

In WordPress 3 there is Featured Image functionality. How do i fetch all posts that have a featured image with them? Here is my current custom loop:

$loop = new WP_Query( array( 'posts_per_page' => 15 ) );
like image 600
Giljed Jowes Avatar asked Aug 09 '10 07:08

Giljed Jowes


People also ask

How do I fetch featured image in WordPress?

To add a featured image in a WordPress post, simply edit or create a new blog post. In the content editor, you'll find the featured image tab in the right column. You need to click on the 'Set Featured Image' area, and this will bring up the WordPress media uploader popup.

How do I fetch all posts in WordPress?

The most appropriate use for get_posts is to create an array of posts based on a set of parameters. It retrieves a list of recent posts or posts matching this criteria. get_posts can also be used to create Multiple Loops, though a more direct reference to WP_Query using new WP_Query is preferred in this case.

Where are WordPress featured images stored?

The specific folder where the image files are stored in WordPress is called the uploads folder located inside the /wp-content/ folder. Inside the uploads folder, your media files are stored by year and month folders. Additionally, you'll also see folders created by your WordPress plugins to save other uploads.


1 Answers

This should work:

$loop = new WP_Query( array( 'posts_per_page' => -1, 'meta_key' => '_thumbnail_id' ) );

I haven't tested this, though. Also, this will probably fetch all posts and pages. Use 'post_type' => 'post' to limit it to blog posts.

like image 137
John P Bloch Avatar answered Oct 02 '22 15:10

John P Bloch