set_post_thumbnail( int|WP_Post $post, int $thumbnail_id )

Set a post thumbnail.


Description Description


Parameters Parameters

$post

(int|WP_Post) (Required) Post ID or post object where thumbnail should be attached.

$thumbnail_id

(int) (Required) Thumbnail to attach.


Top ↑

Return Return

(int|bool) True on success, false on failure.


Top ↑

Source Source

File: wp-includes/post.php

function set_post_thumbnail( $post, $thumbnail_id ) {
	$post = get_post( $post );
	$thumbnail_id = absint( $thumbnail_id );
	if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
		if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) )
			return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
		else
			return delete_post_meta( $post->ID, '_thumbnail_id' );
	}
	return false;
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.1.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

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