Tuesday 5 November 2013

Bulk Remove Featured Images From WordPress Posts

Important: All scripts hosted on widcraft.googlecode.com don't work anymore because Google has blocked that SVN repository.
By default, there's no such option in WordPress to remove featured image from all posts at once. You can only remove the featured image by editing each post and removing it manually. It can take a lot of time. For example, it's almost impossible to manually remove featured images from my WWEFansNation.com with more than 4,000 posts. But that's all right cuz we're here to solve this problem.

WPBeginner found a great way to remove (unset) featured images from all posts in bulk. It's worth nothing that this code will only remove the featured image from the posts, and the image will not be deleted from your site.

Copy & paste following code in your theme's functions.php file:

global $wpdb;

$attachments = $wpdb->get_results( "
     SELECT *
     FROM $wpdb->postmeta
     WHERE meta_key = '_thumbnail_id'
" );

foreach ( $attachments as $attachment ) {
    wp_delete_attachment( $attachment->meta_value, true );
}

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

Important: Please remove this code immediately after saving your functions.php file. You only need to run this code once, there's no need to keep this code in your theme.
Important: Check our new website TricksPanda.com for WordPress tutorials, plugins and more.
 
Powered by Blogger.