get_post_thumbnail_id( int|WP_Post $post = null )

Retrieve post thumbnail ID.


Parameters Parameters

$post

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

Default value: null


Top ↑

Return Return

(int|string) Post thumbnail ID or empty string if the post does not exist.



Top ↑

Source Source

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

function get_post_thumbnail_id( $post = null ) {
	$post = get_post( $post );
	if ( ! $post ) {
		return '';
	}
	return (int) get_post_meta( $post->ID, '_thumbnail_id', true );
}

Top ↑

Changelog Changelog

Changelog
Version Description
4.4.0 $post can be a post ID or WP_Post object.
2.9.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    Show all attachments for the current post except the Featured Image
    To get all post attachments except the Featured Image, you can use this function with something like get_posts().

    Do this inside The_Loop (where $post->ID is available).

    $args = array(
    	'post_type'   => 'attachment',
    	'numberposts' => -1,
    	'post_status' => 'any',
    	'post_parent' => $post->ID,
    	'exclude'     => get_post_thumbnail_id(),
    );
    
    $attachments = get_posts( $args );
    
    if ( $attachments ) {
    	foreach ( $attachments as $attachment ) {
    		echo apply_filters( 'the_title', $attachment->post_title );
    		the_attachment_link( $attachment->ID, false );
    	}
    }
    

You must log in before being able to contribute a note or feedback.