admin管理员组

文章数量:1122826

I have a site with loads of posts (news items). Some posts don't have featured images yet. Adding the images to those posts (few hundreds) is a tedious task. We basically want to add 1 and the same image to all the posts without a featured image.

Is there a way to do this in bulk? Like ADD 'this image' to ALL 'posts' (not pages) WHERE 'featured_image' is empty. I really hope there's a plugin for that. Can't find it though.

I have a site with loads of posts (news items). Some posts don't have featured images yet. Adding the images to those posts (few hundreds) is a tedious task. We basically want to add 1 and the same image to all the posts without a featured image.

Is there a way to do this in bulk? Like ADD 'this image' to ALL 'posts' (not pages) WHERE 'featured_image' is empty. I really hope there's a plugin for that. Can't find it though.

Share Improve this question asked May 31, 2022 at 8:45 RickRick 1116 bronze badges 1
  • Requests for plugin recommendations are off topic here - and a plugin wouldn't be the right approach anyway. Modify your code to check for the existence of a featured images, and if there isn't one, show the placeholder. – vancoder Commented Jun 6, 2022 at 22:05
Add a comment  | 

2 Answers 2

Reset to default 2

you can use this plugins as you like :

https://wordpress.org/plugins/default-featured-image/

https://wordpress.org/plugins/auto-post-thumbnail/

The featured image ID is stored in wp_postmeta with a meta_key called _thumbnail_id.

So you can retrieve all posts without a featured image like that:

$args = array(
    'post_type'      => 'post', // Change this to your post type if needed
    'posts_per_page' => -1,     // Get all posts
    'meta_query'     => array(
        array(
            'key'     => '_thumbnail_id',
            'compare' => 'NOT EXISTS', // This checks if the post doesn't have a featured image
        ),
    ),
);

$posts = get_posts($args);

To assign a specific featured image (in this case, image ID 123) to each post do:

foreach ($posts as $post) {

    set_post_thumbnail($post->ID, 123);
    
}

References

https://stackoverflow.com/a/28643825/2456038

本文标签: Bulkadd featured images in posts with no featured image