Get category ID using category name

When coding a WordPress site, you often need to get a category ID. If you ever wanted to be able to get a category ID from the category name, then just read this recipe, I’m pretty sure you will like it.

As usual, let’s start by pasting the function in your functions.php file:

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

Once you saved the file, just call the function with your category name as a parameter. Example:

$category_ID = get_category_id('WordPress Tutorials');

Leave a Reply

Your email address will not be published. Required fields are marked *