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

You appear to be a bot. Output may be restricted

Description

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

Usage

$int|bool = set_post_thumbnail( $post, $thumbnail_id );

Parameters

$post
( int|WP_Post ) required – Post ID or post object where thumbnail should be attached.
$thumbnail_id
( int ) required – Thumbnail to attach.

Returns

int|bool True on success, false on failure.

Source

File name: wordpress/wp-includes/post.php
Lines:

1 to 12 of 12
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;
}
 

 View on GitHub View on Trac