WordPress at a glance

set_post_thumbnail() WP 1.0

Set a post thumbnail.

No Hooks.


Int/true/false. True on success, false on failure.


set_post_thumbnail( $post, $thumbnail_id );
$post(int/WP_Post) (required)
Post ID or post object where thumbnail should be attached.
$thumbnail_id(int) (required)
Thumbnail to attach.

Code of set post thumbnail: wp-includes/post.php VER 5.0.3

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 );
			return delete_post_meta( $post->ID, '_thumbnail_id' );
	return false;

Related Functions

From tag: Thumbnail (images)

More from category: Thumbnails

No comments
    Hello, !     Log In . Register