Nyligen frågade en av våra läsare oss hur de kan bulk remove featured images från inlägg i WordPress? På WPBeginner har vi diskuterat featured images (inläggsminiatyrer) många gånger. Vi visade dig hur du lägger till featured images i WordPress, hur du lägger till new bildstorlekar i WordPress, hur du återskapar thumbnails, vi visade dig även hur du lägger till flera featured images till ett inlägg i WordPress. I den här artikeln kommer vi att visa dig hur du bulk tar bort utvalda bilder från inlägg i WordPress.
Problemet med bulk removal av inläggsminiatyrer eller featured images
Imagely, som standard kan du bara ta bort featured images genom att editera varje inlägg och ta bort den featured image. Nu om en användare har hundratals inlägg med utvalda bilder, kan det vara en mycket tidskrävande uppgift att ta bort dem en efter en. Istället kommer vi att prova ett annat tillvägagångssätt. Vi kommer att köra en databasfråga och ta bort de utvalda bilderna på alla inlägg.
Innan du fortsätter, vänligen notera att koden under kommer att ta bort featured images från alla inlägg på din WordPress site genom att helt enkelt bara klistra in. Observera också att den här koden inte kommer att delete några av dina uppladdade images, de kommer fortfarande att vara available i mediabiblioteket och du kan återanvända dem när som helst.
Allt du behöver göra är att copy and paste den här koden i ditt temas functions.php-fil
.
global $wpdb; $wpdb->query( " DELETE FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' " );
Det är allt. Så snart du har sparat din functions.php-fil kommer detta skript att run en sökning i databasen och ta bort utvalda images från alla inlägg.
Viktigt: Vänligen remove denna kod omedelbart efter att du har saved din functions.php-fil. Du kommer ej att kunna ställa in featured images i WordPress eftersom den här koden kommer att fortsätta att ta bort featured images från inlägg.
Vi hoppas att den här artikeln hjälpte dig att save lite tid och allow you to bulk remove featured images from WordPress posts. För frågor och återkoppling vänligen lämna en comment under.
källa: Kaiser
Syed Balkhi says
Hey WPBeginner readers,
Did you know you can win exciting prizes by commenting on WPBeginner?
Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
You can get more details about the contest from here.
Start sharing your thoughts below to stand a chance to win!
Gregh777 says
This little line of code saved me a lot of hassle. Thanks for that! I had imported about 50 articles and WordPress was saying each one had a featured image when it did not have one.
I already know why it was doing that…long story…however, how am I to remove the featured image when there is nothing to remove?? Just paste these lines of code into the functions.php file like the article says and voila! All ghost images removed
wpbeginner makes me SO happy
WPBeginner Support says
Glad our article could help
Administratör
Asfianda says
Thanks, this so helpfull for me.
WPBeginner Support says
You’re welcome, glad our content could be helpful
Administratör
David says
Thank you.
hercules says
Deleting all the server images assigned to the thumbnails is relatively simple, now, how to condition to delete all uploads not used by the server as featured images? This should be the most interesting, clean the images not used by the system, in order to polish the machine resources deleting junk images, not used.
Peter says
Yes! So helpful, thank you!!!
aman says
it works thank you , but what if i want to remove the post that don’t have feature image , how can i do that ?
Imbert says
Thanks a lot, you really rocks guys!!!
lszllvnt says
it works ! just don’t use with child theme.
Em says
Followed the instructions to the letter. Got this error message: Parse error: syntax error, unexpected ’$wpdb’ (T_VARIABLE) in /home/ruznnjep/public_html/wp-content/themes/natalie/functions.php on line 411
Blog broken. Had to reinstall. Thanks.
Dan says
Thank you! It is an excellent advice to remove all featured images
Jax says
Is there anyway to undo this? I didnt realise it would remove thumbnail images as well. I have tried restoring the thumbnails but it is not working. Thanks
Mike says
#1. Are we 100% positive that the current code does NOT delete any images from my media folder? And simply ”unset” them from featured image?
#2. You mention ”However, the query can be modify to exclude certain post types.” Can you share the code I would use to avoid a category with ID=25?
Thank you so much,
Mike
RaviTeja Adibhatla says
Works like charm. Thanks man
Afzal says
Thanks Buddy… this is what i need … Thanks A lot for this helping tutorials..
Love you bro
srijita chattopadhyay says
THANK YOU!
Raakib Hasan says
This code remove the post thumbnails as well. But I want to keep the post thumbnail ( when it show grids on Homepage) and just want to remove the feature image from inside the posts. Is this possible?
Luke Melbourne says
Awesome! Just what I needed. Thank you.
Karen says
Thank you so much for this tutorial! It worked perfectly!
Zaid Sparrow says
Hi, will I have my all featured images back on place after removing this code? or I’ll have to set them manually again?
Abdul says
this is exactly what i was looking for but i have tried these and it’s not working for me.i am using generate child theme and on my home page some posts are still showing featured image.i also turned them off in genesis settings but nothing happened.how do i stop featured images to be shown on blog page?Thanks
Irmina Santaika says
Thank you so much!
Abhishek Mishra says
Sorry dude, its now working
w0ngsimp4ng says
thanks its work :). I Have removed all featured image and rebuild again with auto post thumbnail plugin.
Jeff says
Hi,
I pasted the above code into my site with no luck. Is this thread still accurate and active?
thanks,
Jeff
Marcelo says
After delete my related images my images from homepage desappeared, but when I go to post details it appears normally. Before run the script, the homepage used the images from inside the post and not from related images, so I really don’t know why they desappeared from my homepage. Do you have any idea why this happened?
Robin Solanki says
Thanks a lot, you helped me saved a lot of time.
Rahul says
Is there a plugin or codes to remove both post and images attached to a post when we delete it? I mean if i delete a post i also want to delete images related to that post also.
WPBeginner Support says
You will have to delete images from media library.
Administratör
Rahul says
Wish someone would have made a plugin like that because deleting images from 100-200 posts is very time consuming… ?? if any plugin of that kind comes up pls do update it in ur site.. thanks for your reply
Nicholas says
Is it possible to query a specific category and remove just those categories posts featured images?
Biriisi says
Thanks man!!
Justin Edwards says
BEWARE
I used this code yesterday to try to delete the featured images as part of an upgrade to a new WordPress theme. While it did delete the featured images from posts, it also deleted them from the media library and I think it’s also deleted the original files from the server.
Having used this code to try and speed thing up, I now find myself having to re-install and re-link over 2,500 images for my site.
WPBeginner Support says
Justin, we tested the code again. It does not delete files from your media library or your server. It just unsets featured images.
Administratör
Sudip says
I am facing the same problem. After adding this code, few images are being erased from media library.
WPBeginner Support says
Sudip, thank you for notifying us. We have updated the article. Please try the new code instead.
UaMV says
One should also note that this deletes featured images not just from standard post, but from all posts (including custom post types), correct?
WPBeginner Support says
Yes this is correct. However, the query can be modify to exclude certain post types.
Administratör
James DiGioia says
Wouldn’t it be easier to just run a query on DB directly?
WPBeginner Support says
Yep, and this is exactly what this code is doing.
Administratör