How to Get Category ID by Name in WordPress

2 from 5
from 11 user
(Rate this post)
How to Get Category ID by Name in WordPress

get_cat_ID function in WordPress is responsible for retrieving category ID from a given category name. I'm going to try the get_cat_ID function in an example to make the usage more clear.

Several ways to get a category ID based on a given category name are available when developing WordPress.

But the most preferred function, in this case, is the get_cat_ID function.

The get_cat_ID function only gets the category name as a parameter, and as a result, it will return an integer ID number.

If the specified category does not exist, the get_the_ID function returned value will be 0.

How to use get_cat_ID function in WordPress?

The get_cat_ID function has an easy syntax and fast usage.

get_cat_ID function syntax in WordPress

The function syntax is super clear. it only accepts one parameter, which is the name of the category (string):

get_cat_ID($categoryName)

get_cat_ID function Example

For a fresh WordPress installation, I want to try the “uncategorized” category name to check if this function works correctly.

$categoryName = "uncategorized";
$categoryId = get_cat_ID($categoryName); //value was 1!

After running the above codes, $categoryId in my case was 1, which is a correct value for my new WordPress installation.

For more information about working with WordPress categories, I recommend you read two articles in my blog:

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

How to Query WordPress Categories, Tags and Taxonomies

What do you think about "How to Get Category ID by Name in WordPress"?

Your rating submit was successfull.

Your rating submit was not successfull.

Menu