Home » How to Remove Featured Image When Deleting a WordPress Post

How to Remove Featured Image When Deleting a WordPress Post

by John Christopher
0 comment
how to remove featured image when deleting a wordpress post

Are you thinking and looking for a method to remove featured image when deleting a wordpress post with wp_delete_attachment? While there’s probably a plugin for this, this quick code snippet has been created for you, you can use it to remove featured image when deleting a post in WordPress.

When you delete a post or page, the featured image that you’ve attached to it will remain there on your website. If you want to remove the featured image attachments as well while deleting the post or page, then keep in mind:

  • If the same featured image is attached to multiple posts, then permanently deleting any of those posts will delete the featured image as well.
  • The images will only be removed when you permanently delete the post from trash.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

banner
add_action( 'before_delete_post', 'wps_remove_attachment_with_post', 10 );
function wps_remove_attachment_with_post($post_id)
{
 
    if(has_post_thumbnail( $post_id ))
        {
      $attachment_id = get_post_thumbnail_id( $post_id );
      wp_delete_attachment($attachment_id, true);
    }
 
}

The above will help you delete your wiordpress featured image from every of your posts you deleted and cleared your wordpress trash bin. please also kindly share this post to your friends if this post helped you to solve this issue.

You may also like

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
logo

Soledad is the Best Newspaper and Magazine WordPress Theme with tons of options and demos ready to import. This theme is perfect for blogs and excellent for online stores, news, magazine or review sites.

Buy Soledad now!

Edtior's Picks

Latest Articles

u00a92022u00a0Soledad.u00a0All Right Reserved. Designed and Developed byu00a0Penci Design.

0
Would love your thoughts, please comment.x
()
x