WordPress at a glance

set_post_format() WP 1.0

Assign a format to a post

Возвращает

Array/WP_Error/false. Array of affected term IDs on success. WP_Error on error.

Works based on: wp_set_post_terms()

No Hooks.

Usage

set_post_format( $post, $format );
$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.

Список изменений

С версии 3.1.0 Введена.

Code of set_post_format() WP 5.5.1

wp-includes/post-formats.php
<?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' );
}

Related Functions

From tag: post format

No comments
    Log In . Register