admin管理员组文章数量:1125974
How do I search the database to find out which articles are missing a featured image on them? I looked around in the wp_posts
table and searched post_type
is equal to article. It gives me a list of all articles but the guid filed has images and no way to see which ones are empty. Does anyone know how I can achieve this?
How do I search the database to find out which articles are missing a featured image on them? I looked around in the wp_posts
table and searched post_type
is equal to article. It gives me a list of all articles but the guid filed has images and no way to see which ones are empty. Does anyone know how I can achieve this?
2 Answers
Reset to default 3I guess you want it to know in which article you need to edit and add featured image.
You can create some file in the worpdress root folder and just use WP_Query
to get all the posts and to check if there is no feature image you can use the has_post_thumbnail()
.
Change the post_type
if needed.
Example
<?php
require_once('./wp-load.php');
$query = new WP_Query(array('post_type'=>'post'));
if ( $query->have_posts() ) while ( $query->have_posts() ) : $query->the_post();
if(has_post_thumbnail() === false) {
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br>
<?php
}
endwhile;
If you still want to use the SQL you can create WP_Query
and take the generated SQL. You can also use this WP_Query
like the first method and you don't need to check if has_post_thumbnail()
is false.
$query = new WP_Query(
array(
'post_type'=>'post',
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'NOT EXISTS'
)
)
)
);
echo ($query->request); // Print the SQL
So the SQL for this example after I modify it is a little.
SELECT * FROM wp_posts LEFT JOIN wp_postmeta ON
(wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = '_thumbnail_id' )
WHERE 1=1 AND (wp_postmeta.post_id IS NULL) AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date
SELECT guid
FROM wp_posts
WHERE post_type = "post"
AND ID NOT IN (
SELECT post_id as `ID`
FROM wp_postmeta
WHERE meta_key = "_thumbnail_id"
)
ORDER BY `wp_posts`.`guid` ASC;
What's happeninng here?
- Find all the entries in the wp_posts table that are for the post type "Post"
- Look for all existing post meta entries for the Featured Image, grab me all of those IDs
- Now filter out the IDs from step 2
The results left over are for posts that don't have a featured image
本文标签: postsHow do I find which articles are missing a featured image in The WordPress database
版权声明:本文标题:posts - How do I find which articles are missing a featured image in The WordPress database? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736641407a1946002.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论