How to Bulk Remove Featured Images in WordPress

In WordPress, you can only remove post featured image one by one by editing each post and clicking on the “Remove Featured Image” link, which  is easier to do if you have only few posts but if you have hundreds of posts then removing them one by one can be a daunting task.  But fear not, there is a simpler and easier way to bulk remove  featured image in WordPress.

Copy the following code and paste it in your theme’s functions.php file.

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

Note: This code will only remove the featured image from the posts, It will not delete the images from your Media Library or from your uploads folder.

Copy the code and open your site’s Dashboard  and it will immediately remove all the featured image.  After that, please go back to your theme’s functions.php and remove the code from the file or else it will keep removing the featured images afterwards.

Hope this article helped you.

How to Show Default Image if Featured Image is not Added

The “the_post_thumbnail()” function for featured image in WordPress does not shows anything if a featured image is not attached to the post. In case you want to show a default image in your theme if a user has not attached any featured image then use following script:

     <a href="<?php the_permalink(); ?>"> // adds a hyperlink to the image
        <?php
        if ( has_post_thumbnail() ) { //check if featured image is added or not
            the_post_thumbnail( 'medium', array( 'class' => 'img-responsive' )  );  //echos featured image if added
        } 
        else {
        ?>
        <img src="<?php echo get_template_directory_uri(); ?>/images/defaultimg.png" class="img-responsive" alt="">  // outputs default image if not . change the url to match your image.                              
        <?php } ?>
    </a>