set_post_format() WordPress Function

The set_post_format() function sets the post format for a given post.

set_post_format( int|object $post, string $format ) #

Assign a format to a post


Parameters

$post

(int|object)(Required)The post for which to assign a format.

$format

(string)(Required)A format to assign. Use an empty string or array to remove all formats from the post.


Top ↑

Return

(array|WP_Error|false) Array of affected term IDs on success. WP_Error on error.


Top ↑

More Information

See Post Formats page for supported formats.


Top ↑

Source

File: wp-includes/post-formats.php

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


Top ↑

Changelog

Changelog
VersionDescription
3.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.