Retrieves the post thumbnail ID.
Parameters
Source
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 );
}
Hooks
- apply_filters( ‘post_thumbnail_id’,
int|false $thumbnail_id ,int|WP_Post|null $post ) Filters the post thumbnail ID.
Note that the return value is typically a string, not an integer (have not yet seen the function return an integer).
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).
To set the post thumbnail, use
set_post_thumbnail
:If you would like to retrieve the post thumbnail id outside of WP_Query Object and can’t get it by the
get_post_thumbnail_id()
function, you could use the below code.