How to Get Post Format by Post ID in WordPress

5 from 5
from 2 user
(Rate this post)
How to Get Post Format by Post ID in WordPress

Check out this WordPress tutorial if you want to learn how to retrieve the format of a post using a post ID. WordPress has a great function for post format detection; it calls get_post_format. We're going to review this function in a Practical example.

The get_post_format function is responsible to retrieve the format of a WP post. This functionality has been added to WordPress since version 3.1.

get_post_format function syntax

This is the syntax when you want to use the get_post_format function of WordPress (keep reading for the practical example):

get_post_format(int|WP_Post|null $post = null)

If you do not specify the optional $post parameter for this function, or you just pass null, get_post_format will retrieve the post format of the current queried WP post (current post in the loop or in the single.php file).

Example of using the get_post_format function to retrieve a post’s format in WP

We have specified the ID of the post in $postId variable. We used 1 as the post ID, you can change it to any post ID you need or do not specify it for the current queried post (the default value for $post parameter is null).

$postId = 1;
$postFormat = get_post_format($postId);
if($postFormat)
	echo $postFormat;

WordPress default Post Formats

WP has its own post formats; also, developers can add new post formats with specifications they desire.

If you did not add any new post formats to WordPress, either by installing third-party plugins or your own codes, This is the list of possible formats (as a “string” that represents the name of the format):

  • aside
  • chat
  • gallery
  • link
  • image
  • quote
  • status
  • video
  • audio

If you try the function on a not -freshly installed WordPress, you may get other formats too.

What do you think about "How to Get Post Format by Post ID in WordPress"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu