Category ID: How To Get It From Category Name

Steven2 responsesWordPress

Every person who already worked on developing a WordPress-based website have faced this question: How to get a category ID? There are plenty of reason you could need to get a Category ID, for example customizing your pages for a given category.

To easily retrieve a Category ID from a Category name, simply paste the following code in the functions.php file of your WordPress theme:

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

From this point, all you need to do is call this new function when you want to get a Category ID from a Category name (don’t forget to edit the category name):

$cat_ID = get_category_id('Category Name');

Learn how I made $33,543 online in the past 12 months.

Simply input your best email below to get started.

2 Comments

  1. Painless Teething

    Its a good post to be used but can you also suggets a simpler way to find the category as I just need to know it for mentioning it in the Menus page once and do not need to recall the same over and over again.
    Any ways thanks for the post and trying ot help people like me.

Learn how I made $33,543 online in the past 12 months.

Simply input your best email below to get started.

No thanks, I have enough money