WP Get Category Id by Name

There are two way to get the id of a category in WordPress.



You can use both codes easily.

This is the best way to get category id by name

$category_ID = get_cat_ID( $cat_name );

and this is other way to get category id by name

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

Comments are closed.