set_post_thumbnail() WordPress Function

The set_post_thumbnail() function is used to set a post's thumbnail image. This function takes two parameters: the post ID and the thumbnail image ID. The post ID is the ID of the post for which you want to set the thumbnail image. The thumbnail image ID is the ID of the image you want to use as the thumbnail image.

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

Sets the post thumbnail (featured image) for the given post.


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

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


Top ↑

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
VersionDescription
3.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More