get_post_thumbnail_id() WordPress Function

The get_post_thumbnail_id() function is used to get the id of a post's featured image. This can be useful if you need to get the id of the featured image in order to set a custom size for it, or if you need to get the id for use with other functions.

get_post_thumbnail_id( int|WP_Post $post = null ) #

Retrieves the post thumbnail ID.


Parameters

$post

(int|WP_Post)(Optional) Post ID or WP_Post object. Default is global $post.

Default value: null


Top ↑

Return

(int|false) Post thumbnail ID (which can be 0 if the thumbnail is not set), or false if the post does not exist.


Top ↑

More Information

  • To enable featured images, see post thumbnails, the current theme must include add_theme_support( 'post-thumbnails' ); in its functions.php file. See also Post Thumbnails.
  • “Post Thumbnail” is an outdated term for “Featured Image”. This function returns the ID of the post’s featured image. It does not return IDs of other images attached to posts that are thumbnail sized.

Top ↑

Source

File: wp-includes/post-thumbnail-template.php

function get_post_thumbnail_id( $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$thumbnail_id = (int) get_post_meta( $post->ID, '_thumbnail_id', true );

	/**
	 * Filters the post thumbnail ID.
	 *
	 * @since 5.9.0
	 *
	 * @param int|false        $thumbnail_id Post thumbnail ID or false if the post does not exist.
	 * @param int|WP_Post|null $post         Post ID or WP_Post object. Default is global `$post`.
	 */
	return (int) apply_filters( 'post_thumbnail_id', $thumbnail_id, $post );
}


Top ↑

Changelog

Changelog
VersionDescription
5.5.0The return value for a non-existing post was changed to false instead of an empty string.
4.4.0$post can be a post ID or WP_Post object.
2.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.