set_post_format()
Sets the format for a post.
The post format is used by the theme to determine the styling of the post: for example, gallery, quote, video etc.
The functionality of formats in WP is organized through taxonomies. Each format is a term of the post_format taxonomy, to which a post is attached.
Support for formats must be added separately via add_theme_support( 'post-formats' )
More about post formats.
Use get_post_format(), has_post_format() to get or check the post format.
Uses: wp_set_post_terms()
No Hooks.
Returns
Array|WP_Error|false.
Array of IDs— affected terms of thepost-formattaxonomy.- WP_Error — an incorrect post ID was specified.
false— the update failed.
Usage
set_post_format( $post, $format );
- $post(int/object) (required)
- ID or object of the post for which to set the post format.
- $format(string/array) (required)
The post format that will be set for the post.
To remove all formats, specify an empty string:
''.Supported formats:
Format Purpose aside A short post, usually without a title gallery A gallery of images link A link to an external site image A single image quote A quote, often inside <blockquote> status A short status, like a tweet video A video or video playlist audio An audio file or podcast chat Chat log: "Name: message"
Examples
#1 Set post format
Set the format gallery for the current post (in the loop):
set_post_format( $post->ID, 'gallery' );
Changelog
| Since 3.1.0 | Introduced. |
set_post_format() set post format code WP 6.9.1
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' );
}