There are situations when we want to delete the post thumbnail image along with when we are deleting the post permanently.
If we do not do so, the media is present and takes up storage space.
Here is a code snipped that will delete the post thumbnail when we delete the post.
add_action( 'wp_trash_post', 'delete_post_permanently' );
function delete_post_permanently( $post_id ){
wp_delete_post($post_id, true); // deletes post
if( has_post_thumbnail( $post_id ) )
{
$attachment_id = get_post_thumbnail_id( $post_id );
wp_delete_attachment($attachment_id, true);
}
}
Comment if this work out and helps you out.
0 Comments