How To Get Category ID Using Category Slug in WordPress?

Few days before I had written a post by explaining a simple method for getting category id using category name in WordPress. Today let us study an easy way to get category id using category slug.

Get category by slug function is an easy method for retrieving category id using category slug.

How To Use?

<?php get_category_by_slug( $cat-slug ) ?>

Here $cat-slug is the parameter of this function. So pass your category’s slug instead of $cat-slug .

Example
<?php
$catObj = get_category_by_slug('category-name');
$cat_id = $catObj->term_id;
?>

Now $cat_id will contain the id of your category.
Also read,

About Shino

Shino, the author of this blog is a 28 year old master graduate in Computer Science, a professional programmer and web developer. TechMagics.com is his humble venture to share whatever he knows and learns about programming,technology and web world.

2 thoughts on “How To Get Category ID Using Category Slug in WordPress?

  1. Agnes James

    Thanks for sharing above useful article. The methods are described very simply and clearly, which is very much understanding even for new bloggers. I was searching for same. Finally here I found my perfect solution.
    Thanks a lot!

Leave a Reply