Title: has_post_thumbnail
Published: April 25, 2014
Last modified: February 24, 2026

---

# has_post_thumbnail( int|WP_Post|null $post = null ): bool

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#wp--skip-link--target)

Determines whether a post has an image attached.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#description)󠁿

For more information on this and similar theme functions, check out the [ Conditional Tags](https://developer.wordpress.org/themes/basics/conditional-tags/)
article in the Theme Developer Handbook.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#parameters)󠁿

 `$post`int|[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)
|nulloptional

Post ID or [WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)
object. Default is global `$post`.

Default:`null`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#return)󠁿

 bool Whether the post has an image attached.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#source)󠁿

    ```php
    function has_post_thumbnail( $post = null ) {
    	$thumbnail_id  = get_post_thumbnail_id( $post );
    	$has_thumbnail = (bool) $thumbnail_id;

    	/**
    	 * Filters whether a post has a post thumbnail.
    	 *
    	 * @since 5.1.0
    	 *
    	 * @param bool             $has_thumbnail true if the post has a post thumbnail, otherwise false.
    	 * @param int|WP_Post|null $post          Post ID or WP_Post object. Default is global `$post`.
    	 * @param int|false        $thumbnail_id  Post thumbnail ID or false if the post does not exist.
    	 */
    	return (bool) apply_filters( 'has_post_thumbnail', $has_thumbnail, $post, $thumbnail_id );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post-thumbnail-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/post-thumbnail-template.php#L25)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/post-thumbnail-template.php#L25-L39)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#hooks)󠁿

 [apply_filters( ‘has_post_thumbnail’, bool $has_thumbnail, int|WP_Post|null $post, int|false $thumbnail_id )](https://developer.wordpress.org/reference/hooks/has_post_thumbnail/)

Filters whether a post has a post thumbnail.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#related)󠁿

| Uses | Description | 
| [get_post_thumbnail_id()](https://developer.wordpress.org/reference/functions/get_post_thumbnail_id/)`wp-includes/post-thumbnail-template.php` |

Retrieves the post thumbnail ID.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

| Used by | Description | 
| [get_oembed_response_data_rich()](https://developer.wordpress.org/reference/functions/get_oembed_response_data_rich/)`wp-includes/embed.php` |

Filters the oEmbed response data to return an iframe embed code.

  | 
| [WP_Media_List_Table::column_title()](https://developer.wordpress.org/reference/classes/wp_media_list_table/column_title/)`wp-admin/includes/class-wp-media-list-table.php` |

Handles the title column output.

  | 
| [prepend_attachment()](https://developer.wordpress.org/reference/functions/prepend_attachment/)`wp-includes/post-template.php` |

Wraps attachment in paragraph tag before content.

  | 
| [get_post_class()](https://developer.wordpress.org/reference/functions/get_post_class/)`wp-includes/post-template.php` |

Retrieves an array of the class names for the post container element.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.4.0](https://developer.wordpress.org/reference/since/4.4.0/) | `$post` can be a post ID or [WP_Post](https://developer.wordpress.org/reference/classes/wp_post/) object. | 
| [2.9.0](https://developer.wordpress.org/reference/since/2.9.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 6 content](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#comment-content-1864)
 2.    [Webmaster](https://profiles.wordpress.org/mattstone-plx/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/has_post_thumbnail/#comment-1864)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F%23comment-1864)
     Vote results for this note: 9[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F%23comment-1864)
 4.  It’s worth noting that [has_post_thumbnail()](https://developer.wordpress.org/reference/functions/has_post_thumbnail/)
     does not just check for the Featured Image as the Codex User Contributed Note 
     suggests. If a post contains no defined featured image but it does contain an 
     image in the content this function will still return TRUE.
 5.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F%3Freplytocom%3D1864%23feedback-editor-1864)
 6.   [Skip to note 7 content](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#comment-content-1951)
 7.    [Phil McDonnell](https://profiles.wordpress.org/philmcdonnell/)  [  9 years ago  ](https://developer.wordpress.org/reference/functions/has_post_thumbnail/#comment-1951)
 8.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F%23comment-1951)
     Vote results for this note: 4[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F%23comment-1951)
 9.  @Matt Stone, I don’t agree with you. Just tested this:
 10.     ```php
         <?php if (has_post_thumbnail( $post->ID ) ): ?>
         ```
     
 11. and it is only showing me true if I do in fact have a featured image. If I don’t
     have one, even with images in the content it returns false…
 12.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F%3Freplytocom%3D1951%23feedback-editor-1951)
 13.  [Skip to note 8 content](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#comment-content-482)
 14.   [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/has_post_thumbnail/#comment-482)
 15. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F%23comment-482)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F%23comment-482)
 16. This example first checks if there is a Post Thumbnail (aka Featured Image) set
     for the current queried item. If there is a Post Thumbnail set, it returns the
     Post Thumbnail. If not, it echoes out a default image which should be located 
     in the current theme’s image folder (assuming the folder is in the theme’s root
     directory).
 17.     ```php
         <?php
         // Must be inside a loop.
     
         if ( has_post_thumbnail() ) {
         	the_post_thumbnail();
         }
         else {
         	echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) 
         		. '/images/thumbnail-default.jpg" />';
         }
         ?>
         ```
     
 18. You can use `set_post_thumbnail_size()` to set a default size for your thumbnail.
     Alternatively, you can add new image sizes to the defaults by use `add_image_size()`.
 19.  * `if ( has_post_thumbnail() ) { the_post_thumbnail( 'post-thumbnails', array('
        class' => 'h-full w-full', 'title' => 'Pantera' ) ); } else { echo ''; }`
      * [Pantera Negra](https://profiles.wordpress.org/simbionte/) [5 years ago](https://developer.wordpress.org/reference/functions/has_post_thumbnail/#comment-4664)
 20.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F%3Freplytocom%3D482%23feedback-editor-482)
 21.  [Skip to note 9 content](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#comment-content-4161)
 22.   [David Elstob](https://profiles.wordpress.org/davwaiguy/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/has_post_thumbnail/#comment-4161)
 23. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F%23comment-4161)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F%23comment-4161)
 24.     ```php
         <?php 
         if ( has_post_thumbnail() ) {
             $attachment_image = wp_get_attachment_url( get_post_thumbnail_id() );
             echo '<link rel="preload" as="image" href="' . esc_attr( $attachment_image ) . '">';	
         } ?>
         ```
     
 25. This checks to see if there is an active thumbnail and preloads the image. Place
     code in the head section of header.php and it will save adding long arrays. It
     should also help with overcoming the Core Web Vitals LCP metric.
 26.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F%3Freplytocom%3D4161%23feedback-editor-4161)
 27.  [Skip to note 10 content](https://developer.wordpress.org/reference/functions/has_post_thumbnail/?output_format=md#comment-content-4510)
 28.   [jakeparrotta](https://profiles.wordpress.org/jakeparrotta/)  [  5 years ago  ](https://developer.wordpress.org/reference/functions/has_post_thumbnail/#comment-4510)
 29. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F%23comment-4510)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F%23comment-4510)
 30.     ```php
         if ( has_post_thumbnail() ) {     
             echo '<div style='background-image: url(' . get_the_post_thumbnail_url() . ');></div>';
         }
         ?>
         ```
     
 31. This if statement checks to see if there is a thumbnail in the post. If not, nothing
     will display.
 32. Use case: A hand-coded loop of posts where some have featured images and some 
     don’t and you don’t want an empty background for that div.
 33.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F%3Freplytocom%3D4510%23feedback-editor-4510)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fhas_post_thumbnail%2F)
before being able to contribute a note or feedback.