In this post I will discuss how to bulk delete featured images from posts. One can manually remove featured images but it is time consuming and might not be practical for websites with large number of posts. So in this post I will share a few code snippets which will make the task of removing featured images a breeze. More specifically we will cover following in this tutorial.
- Removing Featured Images from All Posts / Pages
- Removing Featured Images from all posts belonging to a Category.
- Removing Featured Images from a particular Post Type.
- Removing Featured Images from All Posts published before / after certain date
Take note that we will not delete the featured images, we will simply unset / unlink them. In other words the featured images you remove will still be accessible via the media library.
Lets start
Remove Featured Images from All Posts
Just copy and paste the following snippet in your functions.php file. This code unlinks the featured images. Take note that the images will still be there in the Media Library.
It is best to comment this code once the jobs is done. Otherwise you wont be able to add any featured images.
add_action ('init', 'remove_featured_images' );
function remove_featured_images () {
global $wpdb;
// The following query will only unset the featured image, it will not delete it.
$wpdb->query( "
DELETE FROM $wpdb->postmeta
WHERE meta_key = '_thumbnail_id'
" );
}
Remove Featured Images from Posts belonging to a certain Category
We will utilize get_posts function get posts belonging to a particular category. We will then loop over the posts and unset the featured image one by one. Have a look at the following snippet.
add_action ('init', 'remove_featured_images' ); function remove_featured_images () { global $post; global $wpdb; $args = array('category' => 2); // You can pass id of the category here $myposts = get_posts ($args); foreach ($myposts as $mypost) { $wpdb->query ("DELETE FROM $wpdb->postmeta WHERE post_id = $mypost->ID AND meta_key = '_thumbnail_id' "); } }
Remove Images from a Particular Post Type
This snippet will only remove featured images from Pages and not posts. You can also modify this snippet to unset featured images from any of the custom post types.
add_action ('init', 'remove_featured_images' ); function remove_featured_images () { global $post; global $wpdb; $args = array('post_type' => 'page'); // You can set post_type parameter here $myposts = get_posts ($args); foreach ($myposts as $mypost) { $wpdb->query ("DELETE FROM $wpdb->postmeta WHERE post_id = $mypost->ID AND meta_key = '_thumbnail_id' "); } }
Remove Featured Images from Post Published before / after after certain date.
In this snippet we remove the featured images from all posts published before 10th September 2014. Take note that we first convert the date in unix time-stamp and then compare the time stamp. For more information about strtotime , read the official documentation
add_action ('init', 'remove_featured_images' ); function remove_featured_images () { global $post; global $wpdb; $date_compare = "10 september 2014"; // enter the date in this variable $date_compare_timestamp = strtotime($date_compare); // convert the date into unix time stamp // Now get the Posts $myposts = get_posts (); foreach ($myposts as $mypost) { // Convert Published date into unix timestamp $post_date_timestamp = strtotime($mypost->post_date); // Remove featured image from post published before 10th September 2014 if ($date_compare_timestamp >= $post_date_timestamp ) { $wpdb->query ("DELETE FROM $wpdb->postmeta WHERE post_id = $mypost->ID AND meta_key = '_thumbnail_id' "); } } }
Conclusion
The beauty of wordpress is that it is simple yet so flexible. The get_posts function is really powerful and allows you to customize wordpress as you want. I hope you guys like the tutorial. In future posts I will share more snippets for working with Images and Media Files.
- How to create 301 Redirects in WordPress - December 21, 2019
- How to Filter Blog Posts by Author in WordPress Admin panel - October 21, 2019
- How to convert / change post type in wordpress - October 21, 2019
Trying to remove thumbnails for all posts EXCEPT these two categories (cat 262 and cat 57). I wanted to check before I did something disastrous to my DB. Thanks guys.
——————————-
add_action (‘init’, ‘remove_featured_images’ );
function remove_featured_images () {
global $post;
global $wpdb;
$args = array(‘category’ != ‘262’ && ‘category’ != ’57’); // You can pass id of the category here
$myposts = get_posts ($args);
foreach ($myposts as $mypost)
{
$wpdb->query (“DELETE FROM $wpdb->postmeta WHERE post_id = $mypost->ID AND meta_key = ‘_thumbnail_id’ “);
}
}
I thought I already posted a question here, but it threw me an error, so trying again. I am trying to un-attach all feature images from ever post on my site, EXCEPT posts that are within either of these two (262 or 57) categories (among others). i.e. A post might be in 262 + 3, in that case i WOULD want it to retain its feature image. If it’s in only 262, WOULD retain it’s feature. But for any posts that does not have category 262 or 57 anywhere in their categories, strip out the Featured Image.
How should I tweak this code to get that effect?
Thanks SO much!
function remove_featured_images () {
global $post;
global $wpdb;
if (array(‘category’ => -262,-57)) {
$myposts = get_posts ($args);
foreach ($myposts as $mypost)
{
$wpdb->query (“DELETE FROM $wpdb->postmeta WHERE post_id = $mypost->ID AND meta_key = ‘_thumbnail_id’ “);
}
}
}
Thank you – works great 🙂
Thanks man!