WordPress get_post_thumbnail_id() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Returns the image ID

Definition

In WordPress, get_post_thumbnail_id() returns the attachment ID of the featured image of a post. The featured image is also called the post thumbnail.

It returns a number, not an image. Use the number with other functions, such as wp_get_attachment_image(), to build the image yourself. It returns 0 when the post has no featured image.

Syntax

A few ways to call get_post_thumbnail_id().

WordPress
<?php
$image_id = get_post_thumbnail_id();
CallWhat it does
get_post_thumbnail_id()Get the image ID for the current post.
get_post_thumbnail_id( 15 )Get the image ID for the post with ID 15.
get_post_thumbnail_id( get_the_ID() )The same as the first call, written in full.
get_post_thumbnail_id( $post )Pass a WP_Post object.

It returns an integer. It never prints anything, so use echo if you want to see the number.

Full declaration
WordPress
get_post_thumbnail_id( int|WP_Post $post = null ): int|false

Parameters

ParameterDescription
$postint|WP_Post|null. A post ID or post object. Default is null, which uses the current post.

Return Value

Return ValueDescription
intThe attachment ID of the featured image. It is 0 if the post has no featured image.
falseThe post does not exist.

WordPress Examples

Three short examples. Each one shows the code and what it prints.

Example 1 — Get the image ID

Print the attachment ID of the featured image.

WordPress
<?php
$image_id = get_post_thumbnail_id();
echo $image_id;

How It Works

  1. get_post_thumbnail_id() looks at the current post.
  2. It returns the attachment ID of the featured image.
  3. echo prints the number.

Example 2 — Check if an image is set

The ID is 0 when there is no featured image, so you can use it in an if.

WordPress
<?php
$image_id = get_post_thumbnail_id( get_the_ID() );

if ( $image_id ) {
  echo 'Featured image ID: ' . $image_id;
} else {
  echo 'No featured image.';
}

How It Works

  1. 0 counts as false in PHP, so an empty post skips the first block.
  2. A real ID counts as true.
  3. For a simple yes or no, has_post_thumbnail() is easier to read.

Example 3 — Build the image with the ID

Pass the ID to wp_get_attachment_image() and pick a size.

WordPress
<?php
$image_id = get_post_thumbnail_id();

if ( $image_id ) {
  echo wp_get_attachment_image( $image_id, 'medium' );
}

How It Works

  1. The ID comes from get_post_thumbnail_id().
  2. wp_get_attachment_image() returns the img tag for that ID and size.
  3. You can also use wp_get_attachment_image_url() when you need only the URL, for example in a CSS background.

Frequently Asked Questions

It returns the attachment ID of the featured image of a post. It returns 0 if there is no featured image.
Yes. WordPress calls it a post thumbnail in code and a featured image in the editor.
Pass the ID to wp_get_attachment_image_url( $id, 'large' ), or use get_the_post_thumbnail_url().
It returns 0. If the post does not exist, it returns false.
No. It only returns the ID. Use the_post_thumbnail() to print the image.

Browse all thumbnail functions

Open the functions index for simple guides with examples and FAQs.

Thumbnail Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful