WP-Mix

A fresh mix of code snippets and tutorials

WordPress Get Category ID from Slug

Quick WordPress snippet for getting the category ID from the category slug.

Don’t blink or you’ll miss it:

$slug = 'docs';
$cat = get_category_by_slug($slug); 
$catID = $cat->term_id;

Here we pass the $slug to WordPress’ get_category_by_slug() function. That gives us an object of useful category data, including the ID, which we then call directly:

$catID = $cat->term_id;

To see what else is available try adding the following to your script:

print_r($cat);

It’s just good times.

Learn more

WordPress Themes In Depth