Sometime WordPress Developer need to know Category ID, I will show you how to find Category ID using the Category Slug in WordPress, this tricks is original by Bavotasan and I rewrite it.
WordPress built in with nicely functions get_term_by(), and we will use it to get Category ID based on Category Slug. Here we go what we can do with get_term_by()
<?php get_term_by( $field, $value, $taxonomy, $output, $filter ) ?>
$field: (string) (required) Either ’slug’, ‘name’, or ‘id’. Default: None
$value: (string|integer) (required) Search for this term value. Default: None
$taxonomy: (string) (required) Taxonomy Name. Default: None
$output: (string) (optional) Constant OBJECT, ARRAY_A, or ARRAY_N. Default: OBJECT
$filter: (string) (optional) default is raw or no WordPress defined filter will applied. Default: ‘raw’
Let’s get practice by adding this code to get Category ID based on Category Slug
<?php $theCatId = get_term_by( 'slug', 'tutorials', 'category' ); $theCatId = $theCatId->term_id; ?>
What’s for this Category ID? It depend on you, you can use it to add extra query or display category description. Next code is how to display category description using Category Slug
<?php $theCatId = get_term_by( 'slug', 'tutorials', 'category' ); $theCatId = $theCatId->description; ?>
Of course you can make this code more useful, use your creativity and explore wordpress tricks and Create better code to get Category ID based on Category Slug. Learn WordPress everyday and you’ll more enjoy with it



I will show you how to find Category ID
Thanks for sharing this, I just used it to sort a menu system out
Its probably been a best way to find the category id in wordpress. I must try to apply it on my blog. I hope i ‘ll definitely get the desired results. Thanks for the tutorial mate.
This seems to be a helpful piece of trick on wordpress. Let me try if this will work on my weblog. Thanks for sharing this.
Great tip! I was wondering if there is anyway to obtain the Post ID from the Post Title or Post Name?
Sorry, Out of topic.
Can wordpress show different post from category depending user click? I mean here, from one single page, it will detect automatically category id and show post from that category.
thank you
Impressive. I haven’t tried this yet. I’ll check out later and see if it’ll work with my blog. Thanks for the information. I appreciate your help
Pingback: Alex Barber | Digital Artist | WordPress get category slug from its name
Let me try if this will work on my weblog.
I hope i ‘ll definitely get the desired results. Thanks for the tutorial mate.