Get Category ID using Category Name

a minute read

There are times when WordPress developers need category data, especially the ID, while developing themes and plugins. It is not an ideal way, but sometimes, you may need to get the category ID using the Category name.

In case you too are in such a situation, here is a quick code snippet to grab the ID of a category by providing the category name.

The Function

We can utilize the get_term_by() function from WordPress core to get the term ID, the term in this case would be our category. The function could be used read the name of term, and to return its ID. Below function executes this idea in just few of lines:

function get_category_id($cat_name){
  $term = get_term_by('name', $cat_name, 'category');
  return $term->term_id;

Include this function in your plugin’s main file, or in case if you are implementing it on a theme, add it in the functions.php file of your theme.


Best practice is to get the returned value from our function in a variable, as shown below:

$category_ID = get_category_id('Code Snippets');

Hope you found it useful.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

The default WordPress search is the only thing that used to annoy me a lot and I wasn’t using it on any of my WordPress sites. There are a number of reasons back then to stop using WordPress search feature, and the best one for me was it throwing unrelated lin...
Icon fonts allow you to add resolution-independent, multicolor icons to your web design projects. There are many icon fonts out there available for free, but Font Awesome is the name that dominates all. Using Font Awesome is pretty simple. After adding all tho...
WordPress automatically adds its current Version number to the head section of the themes. If you view the source of a WordPress-based website, you may find out the WordPress version it is using. Below given is the meta tag that carries that version informatio...