Using WordPress ‘delete_post_thumbnail()’ PHP function

The delete_post_thumbnail() WordPress PHP function removes the thumbnail (also known as the featured image) from a specific post.

Usage

Here’s a simple way to use the delete_post_thumbnail() function. Let’s say you want to remove the thumbnail from a post with the ID of 123. You’d use the function like this:

delete_post_thumbnail(123);

This would remove the thumbnail from the post with the ID of 123.

Parameters

  • $post (int|WP_Post) – Required. This is the post ID or the post object from which the thumbnail should be removed.

More information

See WordPress Developer Resources: delete_post_thumbnail()

Examples

Removing a Thumbnail from a Specific Post

In this example, we’ll remove the thumbnail from the post with the ID of 123.

delete_post_thumbnail(123); // removes thumbnail from post 123

Removing a Thumbnail Inside a Loop

In a WordPress loop, you can use the delete_post_thumbnail() function to remove the thumbnail from each post in the loop.

while ( have_posts() ) : the_post();
    delete_post_thumbnail( get_the_ID() ); // removes thumbnail from the current post in the loop
endwhile;

Removing a Thumbnail from a Post Object

If you have a post object, you can also use delete_post_thumbnail() to remove the thumbnail.

$post_object = get_post(123);
delete_post_thumbnail($post_object); // removes thumbnail from post object

Checking if a Post has a Thumbnail Before Removing it

Before removing a thumbnail, it might be a good idea to check if the post has a thumbnail. You can use the has_post_thumbnail() function for this.

$post_id = 123;
if ( has_post_thumbnail($post_id) ) {
    delete_post_thumbnail($post_id); // removes thumbnail if the post has one
}

Removing a Thumbnail and Displaying a Confirmation Message

In this example, we’ll remove a thumbnail and display a message confirming that the thumbnail was removed.

$post_id = 123;
delete_post_thumbnail($post_id);
echo 'The thumbnail was removed from post ' . $post_id; // displays a confirmation message