set_post_format

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

WordPress Version: 5.5

/**
 * Assign a format to a post
 *
 * @since 3.1.0
 *
 * @param int|object $post   The post for which to assign a format.
 * @param string     $format A format to assign. Use an empty string or array to remove all formats from the post.
 * @return array|WP_Error|false Array of affected term IDs on success. WP_Error on error.
 */
function set_post_format($post, $format)
{
    $post = get_post($post);
    if (!$post) {
        return new WP_Error('invalid_post', __('Invalid post.'));
    }
    if (!empty($format)) {
        $format = sanitize_key($format);
        if ('standard' === $format || !in_array($format, get_post_format_slugs(), true)) {
            $format = '';
        } else {
            $format = 'post-format-' . $format;
        }
    }
    return wp_set_post_terms($post->ID, $format, 'post_format');
}

WordPress Version: 5.1

/**
 * Assign a format to a post
 *
 * @since 3.1.0
 *
 * @param int|object $post   The post for which to assign a format.
 * @param string     $format A format to assign. Use an empty string or array to remove all formats from the post.
 * @return array|WP_Error|false WP_Error on error. Array of affected term IDs on success.
 */
function set_post_format($post, $format)
{
    $post = get_post($post);
    if (!$post) {
        return new WP_Error('invalid_post', __('Invalid post.'));
    }
    if (!empty($format)) {
        $format = sanitize_key($format);
        if ('standard' === $format || !in_array($format, get_post_format_slugs())) {
            $format = '';
        } else {
            $format = 'post-format-' . $format;
        }
    }
    return wp_set_post_terms($post->ID, $format, 'post_format');
}

WordPress Version: 4.3

/**
 * Assign a format to a post
 *
 * @since 3.1.0
 *
 * @param int|object $post   The post for which to assign a format.
 * @param string     $format A format to assign. Use an empty string or array to remove all formats from the post.
 * @return array|WP_Error|false WP_Error on error. Array of affected term IDs on success.
 */
function set_post_format($post, $format)
{
    $post = get_post($post);
    if (empty($post)) {
        return new WP_Error('invalid_post', __('Invalid post.'));
    }
    if (!empty($format)) {
        $format = sanitize_key($format);
        if ('standard' === $format || !in_array($format, get_post_format_slugs())) {
            $format = '';
        } else {
            $format = 'post-format-' . $format;
        }
    }
    return wp_set_post_terms($post->ID, $format, 'post_format');
}

WordPress Version: 3.7

/**
 * Assign a format to a post
 *
 * @since 3.1.0
 *
 * @param int|object $post The post for which to assign a format.
 * @param string $format A format to assign. Use an empty string or array to remove all formats from the post.
 * @return mixed WP_Error on error. Array of affected term IDs on success.
 */
function set_post_format($post, $format)
{
    $post = get_post($post);
    if (empty($post)) {
        return new WP_Error('invalid_post', __('Invalid post'));
    }
    if (!empty($format)) {
        $format = sanitize_key($format);
        if ('standard' === $format || !in_array($format, get_post_format_slugs())) {
            $format = '';
        } else {
            $format = 'post-format-' . $format;
        }
    }
    return wp_set_post_terms($post->ID, $format, 'post_format');
}