How to Get Categories of a Post in WordPress (3 Examples!)

2 from 5
from 13 user
(Rate this post)
How to Get Categories of a Post in WordPress (3 Examples!)

wp_get_post_categories function will help WordPress developers retrieve post categories (Name, ID, Slug, ETC). wp_get_post_categories receives a required Post ID and an optional array of arguments. In this WordPress tutorial, we'll try retrieving Post Categories using the wp_get_post_categories function.

While developing WordPress, It is essential to know how to retrieve a list of categories for a specific post.

WordPress introduced a beneficial function for developers to receive post categories using a post ID.

Introducing wp_get_post_categories function

wp_get_post_categories function receives two parameters, the first one is required, and the second one is optional.

wp_get_post_categories function syntax

wp_get_post_categories function has a mandatory parameter.

The developer must specify the post ID by passing it as the first parameter.

The second parameter is optional, but if you want to specify the parameter, it must have the type of array.

wp_get_post_categories(int $post_id, array $args)

3 Examples of using wp_get_post_categories function

1- Get category IDs of a WordPress post

By default, the wp_get_post_categories function will return an array of category IDs if available:

$postId = 1;
$categoryIds = wp_get_post_categories($postId);

2- Get category names of a WordPress post

Retrieving category names is also an easy task, we must define the second parameter of wp_get_post_categories function which must be an array.

In the defined array we need to specify the field key to tell WordPress what attributes of an array we want to receive?

Example of defining arguments array to receive category names instead of IDs:

$postId = 1;
$args = array("fields"=>"names");
$categoryNames = wp_get_post_categories($postId, $args);

3- Get category objects of a WordPress post (full details)

for full details of categories, like the previous example, you need to specify the field key for arguments array.

If the field key in arguments array is set to all, you will receive an array of WP_Term objects which contain ID, name, slug, description, count, and ETC.

$postId = 1;
$args = array("fields"=>"all");
$categoryObjects = wp_get_post_categories($postId, $args);

A possible result is something like below:

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 1
            [name] => Uncategorized
            [slug] => uncategorized
            [term_group] => 0
            [term_taxonomy_id] => 1
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 4
            [filter] => raw
        )

)

Explaining wp_get_post_categories arguments

What details can I get from wp_get_post_categories function in WordPress?

wp_get_post_categories function lets developers decide what type of data they want to receive from a specifically filtered result list.

You can filter results easily by using available WordPress filters for terms, read available filters for terms on the WordPress official page.

Ordering returned results by wp_get_post_categories function

To order results, you must define order key of arguments array.

Results can be ordered by: ‘name’, ‘slug’, ‘term_group’, ‘term_id’, ‘id’, ‘description’, ‘parent’, ‘term_order’, ‘slug__in’, ‘count’, ‘meta_value’.

For example, we can order categories by name instead of ID, using below code:

$postId = 1;
$args = array(
	"fields" => "all",
	"order" => "ASC",//direction
	"orderby" => "name", //attribute to order by
);
$categoryObjects = wp_get_post_categories($postId, $args);

Filtering post categories returned by wp_get_post_categories function

Filtering results is easy, you can define either one of available filters (read offcial page) in arguments array.

For example to search a category, you can use search key for argument array:

$postId = 1;
$searchName = "uncategorized";//search a name!
$args = array(
	"fields" => "all",
	"search" => $searchName
);
$categoryObjects = wp_get_post_categories($postId, $args);

beside search key, you can use name__like or description__like to specify which attribute you want to search for a word.

Where can I find more information about the wp_get_post_categories function?

Keep reading about categories; there are a lot more details to explore.

I recommend you read the “How to Query WordPress Categories, Tags and Taxonomies” article on my blog.

This article can help you do stuff with categories. Also, it will introduce you to WordPress taxonomies and a lot more!

Also WordPress has the perfect official page for developers about wp_get_post_categories function.

What do you think about "How to Get Categories of a Post in WordPress (3 Examples!)"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu