Dec
31
2009

Find the Category ID using the Category Slug in WordPress

by   |  Posted in Tutorials  |  9 comments

This is a quick little piece of code that I have been using a lot lately. I needed to get the category ID but all I had to work with was the category slug. Luckily WordPress has a neat little function called get_term_by() which makes it super easy. The function can be used to get a lot more information but for this example I am just going to extract the ID.

The function works like this:

<?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’

We are going to use it like this to get the ID with the category slug ‘tutorials’:

<?php 
$theCatId = get_term_by( 'slug', 'tutorials', 'category' );
$theCatId = $theCatId->term_id;
?>

If you insert a print_r() function you can echo out all the information that the get_term_by() function contains. Use something like this and see if there is other information that you might want to extract:

<?php 
$theCatId = get_term_by( 'slug', 'tutorials', 'category' );
print_r($theCatId);
?>

Then all you would have to do is use whichever term your see between those square brackets like so:

<?php 
$theCatId = get_term_by( 'slug', 'tutorials', 'category' );
$theCatId = $theCatId->description;
?>

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
If you liked this, please share it.

Tags: , , , , , , , , , , , , , ,

Short URL: http://bit.ly/ccAlCp

Discussion 9 Comments

  1. Mike Brevoort on January 17, 2010 at 12:59 am

    Exactly what I was looking for. Thanks!

  2. emonweb on February 8, 2010 at 2:59 pm

    Me too. i really hate hard coding the category id directly.

  3. jeyush on April 22, 2010 at 6:48 am

    I have permalinks set up.
    Before permalinks setup I have query string like this,

    http://localhost/wordpress/products-page?category=2&product_id=11

    And by changing it to permalinks to numeric,

    It shows ;
    http://localhost/wordpress/products-page/po-campo1/product-3

    There is
    po-campo1 is category
    product-3 is product in po-campo1 category.

    So, how can I get category_id and product_id at the top of the page?

    If you know that please share it with me, I really need it.

    Thanks.

    • c.bavota on April 22, 2010 at 10:07 am

      If you want to get the cat id and the product id in the URL you will have to do some messing around with your .htaccess file.

  4. Daniele on August 16, 2010 at 11:26 am

    Hard to say why the guys on WP Codex site didn’t added such a little (and useful) piece of code when writing docs about this function. Thanks for your help!

  5. David on December 7, 2010 at 9:39 am

    Thanks so much! This is just what I needed.

  6. get articles on December 17, 2010 at 6:04 pm

    [...] Find the Category ID using the Category Slug in WordPress | bavotasan.com [...]

  7. Chris on March 11, 2011 at 10:00 am

    Where do you put this code?

  8. Demonboy on May 3, 2011 at 6:08 am

    As a non-programmer who only understand the basics of PHP this is a good attempt at an explanation but is not enough. Just cutting and pasting your code into mine, which is how I teach myself stuff like this, does nothing. Please provide some examples of where you would use this, thanks.