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 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
How to use
get_cat_ID function in WordPress?
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 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:
written by Mehdi Nazari about in WordPress