set_post_thumbnail()WP 3.1.0

Sets the post thumbnail based on the provided post ID and attachment (media file) ID. If the specified attachment does not exist in the database, the function will remove the post thumbnail.

No Hooks.

Returns

Int|true|false.

  • true - If the thumbnail was set and the data was updated. Or if delete_post_meta() was successfully executed.
  • false - if the data could not be inserted or if the specified ID is already set.
  • Number - Only when creating a meta-field. The ID of the inserted metadata row (meta_id) - returned by the function update_post_meta().

When inserting (not updating), the returned value is not "true" on success, but "number".

Will return false on the second run. If the image is already set and you are passing the same attachment ID. This happens because update_post_meta() returns false when the value has not changed.

Usage

set_post_thumbnail( $post, $thumbnail_id );
$post(number/object) (required)
ID of the post for which to set the thumbnail. You can pass a post object.
$thumbnail_id(number) (required)
ID of the attachment to be attached.

Examples

0

#1 Set up a thumbnail of the posts

Let's say we want to set a thumbnail for post with ID=56. We know the ID of the media file (attachment) - it is 89:

$post_id = 56;
$attach_id = 89;
$set_thumb = set_post_thumbnail( $post_id, $attach_id );

if( $set_thumb )
	echo 'The miniature is set.';
else
	echo 'The thumbnail has been deleted.';
0

#2 Automatic installation of thumbnail posts

This code will automatically put a thumbnail of the post when you save it. If the post has no thumbnail.

The first picture attached to the post will be grabbed.

add_action( 'future_to_publish', 'autoset_featured' );
add_action( 'draft_to_publish', 'autoset_featured' );
add_action( 'new_to_publish', 'autoset_featured' );
add_action( 'pending_to_publish', 'autoset_featured' );
//add_action( 'the_post', 'autoset_featured' );
add_action( 'save_post', 'autoset_featured' );

function autoset_featured() {
	global $post;

	// check for a post thumbnail
	if( has_post_thumbnail( $post->ID ) )
		return;

	$attached_image = get_children( [
		'post_parent'    => $post->ID, 
		'post_type'      => 'attachment', 
		'post_mime_type' => 'image', 
		'numberposts'    => 1, 
		'order'          => 'ASC',
	] );

	// make a condition to check for a picture
	if( $attached_image ){
		foreach( $attached_image as $attachment_id => $attachment ){
			set_post_thumbnail($post->ID, $attachment_id);
		}
	}
}
0

#3 Programmatically setup an uploaded image file as a thumbnail

/*
 * $file is the path to your uploaded file (for example as set in the $_FILE posted file array)
 * $filename is the name of the file
 * first we need to upload the file into the wp upload folder.
 */
$upload_file = wp_upload_bits( $filename, null, @file_get_contents( $file ) );

if( ! $upload_file['error'] ){
	// if succesfull insert the new file into the media 
	// library (create a new attachment post type).
	$wp_filetype = wp_check_filetype( $filename, null );

	$attachment = [
		'post_mime_type' => $wp_filetype['type'],
		'post_parent'    => $post_id,
		'post_title'     => preg_replace( '/\.[^.]+$/', '', $filename ),
		'post_content'   => '',
		'post_status'    => 'inherit',
	];

	$attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $post_id );

	if( ! is_wp_error( $attachment_id ) ){
		// if attachment post was successfully created, insert it as a thumbnail to the post $post_id.
		require_once( ABSPATH . "wp-admin" . '/includes/image.php' );

		$attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );

		wp_update_attachment_metadata( $attachment_id, $attachment_data );
		set_post_thumbnail( $post_id, $attachment_id );
	}
}

Changelog

Since 3.1.0 Introduced.

set_post_thumbnail() code WP 7.0

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;
}