set_post_format

Assign a format to a post.

Description

(array|WP_Error|false) set_post_format( (int|object) $post, (string) $format ); 

Returns (array|WP_Error|false)

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

Parameters (2)

0. $post (int|object)
The post for which to assign a format.
1. $format (string)
A format to assign. Use an empty string or array to remove all formats from the post.

Usage

  1. if ( !function_exists( 'set_post_format' ) ) { 
  2. require_once ABSPATH . WPINC . '/post-formats.php'; 
  3.  
  4. // The post for which to assign a format. 
  5. $post = null; 
  6.  
  7. // A format to assign. Use an empty string or array to remove all formats from the post. 
  8. $format = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = set_post_format($post, $format); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post-formats.php  
  1. function set_post_format( $post, $format ) { 
  2. $post = get_post( $post ); 
  3.  
  4. if ( empty( $post ) ) 
  5. return new WP_Error( 'invalid_post', __( 'Invalid post.' ) ); 
  6.  
  7. if ( ! empty( $format ) ) { 
  8. $format = sanitize_key( $format ); 
  9. if ( 'standard' === $format || ! in_array( $format, get_post_format_slugs() ) ) 
  10. $format = ''; 
  11. else 
  12. $format = 'post-format-' . $format; 
  13.  
  14. return wp_set_post_terms( $post->ID, $format, 'post_format' );