Bulk remove featured images in in WordPress

Joined
Mar 15, 2018
Messages
53
Likes
10
Degree
0
Hi there,

I need to bulk remove featured images in WordPress.

I found this code, which removes all featured images from every post:

Code:
global $wpdb;
$wpdb->query( "
    DELETE FROM $wpdb->postmeta
    WHERE meta_key = '_thumbnail_id'
" );

It seems to work as intended, but I need to find away to make it conditional. I'd like it to unset only featured images of posts that don't have a certain tag.

Anyone got an idea?
 
Might be easier to to in PHPMyAdmin?


Thanks for the input.

It might very well be easier with your recommendation, assuming I'd have it installed already and knew how to use it. Unfortunately I don't, so I am still hoping someone is able to add a couple lines of code to the snippet above to make it work for what I have in mind.
 
Provided you aren't talking about a huge number of posts, then you should be fine with adding a snippet of code to your `functions.php` file, reloading the site, and then removing it:

PHP:
//takes an array of tag id's: https://mythemeshop.com/blog/how-to-find-post-category-tag-comments-or-user-id-in-wordpress/
$query = ['tag__not_in' => [123]];

$posts_without_tag = get_posts($query);

foreach($posts_without_tag as $p){

    delete_post_thumbnail($p);

}
 
Provided you aren't talking about a huge number of posts, then you should be fine with adding a snippet of code to your `functions.php` file, reloading the site, and then removing it:

Thank you very much for giving it a try. Unfortunately this snippet doesn't seem to do anything, while the one I posted seemed to work fine for the same number of posts. What would you consider a huge number of posts?
 
Thank you very much for giving it a try. Unfortunately this snippet doesn't seem to do anything, while the one I posted seemed to work fine for the same number of posts. What would you consider a huge number of posts?
If there are too many posts, it will just be very slow / throw oom error.

If it's not doing anything, then something else is wrong. Are you certain you used the correct tag ids?
I'm presuming you are not running this locally with access to xdebug?

Anything in your error logs?
 
If it's not doing anything, then something else is wrong. Are you certain you used the correct tag ids?
I'm presuming you are not running this locally with access to xdebug?

Actually it does seem to work, but only for about the first ~15 posts.

Is there a way to write the condition in this instead, as it seemed to work fine for several thousand posts:

Code:
global $wpdb;
$wpdb->query( "
    DELETE FROM $wpdb->postmeta
    WHERE meta_key = '_thumbnail_id'
" );

I found this

Code:
DELETE FROM {$wpdb->postmeta}
WHERE  post_id IN (SELECT post_id
                   FROM   (SELECT post_id
                           FROM   {$wpdb->postmeta}
                           WHERE  meta_key LIKE '_thumbnail_id'
                                  AND post_id IN(SELECT post_id
                                                 WHERE  meta_key LIKE 'rafi'))
                          AS s)

Which seems to be related, but I couldn't figure out yet how to apply it to my case.
 
That worked perfectly, thanks a lot for your help!!!!
 
Back