Home WordPress Thumbnails Functions get_post_thumbnail_id() WordPress get_post_thumbnail_id() Function 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().
<?php
$image_id = get_post_thumbnail_id(); Call What 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 get_post_thumbnail_id( int|WP_Post $post = null ): int|false Parameters Parameter Description $postint|WP_Post|null. A post ID or post object. Default is null, which uses the current post.
Return Value Return Value Description int The attachment ID of the featured image. It is 0 if the post has no featured image. false The post does not exist.
WordPress Examples Three short examples. Each one shows the code and what it prints.
1. Get the image ID 2. Check if an image is set 3. Build the image with the ID Example 1 — Get the image ID Print the attachment ID of the featured image.
<?php
$image_id = get_post_thumbnail_id();
echo $image_id; How It Works get_post_thumbnail_id() looks at the current post.It returns the attachment ID of the featured image. 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.
<?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 0 counts as false in PHP, so an empty post skips the first block.A real ID counts as true. 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.
<?php
$image_id = get_post_thumbnail_id();
if ( $image_id ) {
echo wp_get_attachment_image( $image_id, 'medium' );
} <img width="300" height="200" src="https://example.com/wp-content/uploads/2026/11/photo-300x200.jpg" class="attachment-medium size-medium" alt="" decoding="async" loading="lazy" /> How It Works The ID comes from get_post_thumbnail_id(). wp_get_attachment_image() returns the img tag for that ID and size.You can also use wp_get_attachment_image_url() when you need only the URL, for example in a CSS background. Frequently Asked Questions What does get_post_thumbnail_id() do? It returns the attachment ID of the featured image of a post. It returns 0 if there is no featured image.
Is the featured image the same as the post thumbnail? Yes. WordPress calls it a post thumbnail in code and a featured image in the editor.
How do I get the image URL from the ID? Pass the ID to wp_get_attachment_image_url( $id, 'large' ), or use get_the_post_thumbnail_url().
What does it return if there is no featured image? It returns 0. If the post does not exist, it returns false.
Does it print the image? No. It only returns the ID. Use the_post_thumbnail() to print the image.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion