How to fetch all WordPress posts with featured ima

2019-04-20 04:25发布

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 ) );

2条回答
做自己的国王
2楼-- · 2019-04-20 04:58

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-04-20 05:01

I don't believe you need any special loops defined for that to work.

Though you do need to add some small snippets into your functions.php

like this one:

<?php add_theme_support ( 'post-thumbnails' ); ?>

after apply the above code to functions.php file, your theme will support Featured Images and you will see a new link @ the bottom right of your Post Add / Edit interface.

This guide will help you, if you are looking for more info regarding this : How to Use Wordpress Featured Image Feature

查看更多
登录 后发表回答