set_post_thumbnail

The timeline below displays how wordpress function set_post_thumbnail has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.1

/**
 * Sets the post thumbnail (featured image) for the given post.
 *
 * @since 3.1.0
 *
 * @param int|WP_Post $post         Post ID or post object where thumbnail should be attached.
 * @param int         $thumbnail_id Thumbnail to attach.
 * @return int|bool True on success, false on failure.
 */
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;
}

WordPress Version: 4.3

/**
 * Set a post thumbnail.
 *
 * @since 3.1.0
 *
 * @param int|WP_Post $post         Post ID or post object where thumbnail should be attached.
 * @param int         $thumbnail_id Thumbnail to attach.
 * @return int|bool True on success, false on failure.
 */
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;
}

WordPress Version: 4.0

/**
 * Set a post thumbnail.
 *
 * @since 3.1.0
 *
 * @param int|WP_Post $post         Post ID or post object where thumbnail should be attached.
 * @param int         $thumbnail_id Thumbnail to attach.
 * @return bool True on success, false on failure.
 */
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;
}

WordPress Version: 3.9

/**
 * Sets a post thumbnail.
 *
 * @since 3.1.0
 *
 * @param int|WP_Post $post Post ID or post object where thumbnail should be attached.
 * @param int $thumbnail_id Thumbnail to attach.
 * @return bool True on success, false on failure.
 */
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 ($thumbnail_html = 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;
}

WordPress Version: 3.7

/**
 * Sets a post thumbnail.
 *
 * @since 3.1.0
 *
 * @param int|object $post Post ID or object where thumbnail should be attached.
 * @param int $thumbnail_id Thumbnail to attach.
 * @return bool True on success, false on failure.
 */
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 ($thumbnail_html = 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;
}