Using WordPress ‘has_post_format()’ PHP function

The has_post_format() WordPress PHP function checks if a post has any of the given formats or any format at all.

Usage

has_post_format( $format, $post );

Example:

Input:

if ( has_post_format('gallery', 42) ) {
  // Do something for post with ID 42 and gallery format
}

Output:

Executes code block if post with ID 42 has gallery format.

Parameters

  • $format (string|string[]|null) (Optional) – The format or formats to check. Default: null.
  • $post (WP_Post|int|null) (Optional) – The post to check. Defaults to the current post in the loop. Default: null.

More information

See WordPress Developer Resources: has_post_format

Examples

Check if the current post in the loop has a gallery format and then perform some action.

if ( has_post_format('gallery') ) {
  // Do something for gallery format
}

Check for multiple formats

Check if the current post in the loop has either video or audio format and then perform some action.

if ( has_post_format( array('video', 'audio') ) ) {
  // Do something for video or audio format
}

Check for any format

Check if the current post in the loop has any format assigned and then perform some action.

if ( has_post_format() ) {
  // Do something if post has any format
}

Check format for a specific post ID

Check if a specific post with ID 123 has quote format and then perform some action.

if ( has_post_format('quote', 123) ) {
  // Do something for post with ID 123 and quote format
}

Check format for a WP_Post object

Given a WP_Post object, check if it has the link format and then perform some action.

$post_object = get_post( 456 );

if ( has_post_format('link', $post_object) ) {
  // Do something for post with ID 456 and link format
}