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.
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
add_action( 'before_delete_post', 'wps_remove_attachment_with_post', 10 );
if(has_post_thumbnail( $post_id ))
$attachment_id = get_post_thumbnail_id( $post_id );
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.