WordPress at a glance

set_post_thumbnail() WP 1.0

Set a post thumbnail.

No Hooks.

Return

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

Usage

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 4.9.8

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

Related Functions

From tag: Thumbnail (images)

More from category: Thumbnails

No comments
    Hello, !     Log In . Register