Detect Category ID using the Category Slug in WordPress


Sponsored Links




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

Subscribe to Full RSS Feed

RSS FeedIf you thing this article useful, please consider subscribing to our RSS Feed or e-mail updates and stay updated with us. You can also follow @WPTricksNet on twitter for latest updates.

About WP Tricks

We are WordPress Family, we collected and writed useful tips and tricks for you WordPress. We share it for you...

Related Tips and Trick from Local Data and on The Net

Detect Category ID using the Category Slug in WordPress

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 Read More

WordPress › Support » Purging old-slug redirects

So due to the conflict, WordPress changed the category slug to /link25-2/. thing is even if you type the URL with category id I would much rather detect the main issue and Read More

How to use different header for categories and pages in WordPress

Read More

WordPress › List category posts « WordPress Plugins

ways: Using the category id, the category name or slug and to make it detect the category id of for the List Category Plugin are searched for in your WordPress Read More

Conditional Tags « WordPress Codex

Note the close relation these tags have to WordPress Returns true when the category of posts being displayed is either term_ID 9, or slug "blue-cheese", or name Read More


10 Responses to Detect Category ID using the Category Slug in WordPress

  1. wiper blades says:

    I will show you how to find Category ID

  2. Thanks for sharing this, I just used it to sort a menu system out

  3. 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.

  4. John says:

    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.

  5. Think Well says:

    Great tip! I was wondering if there is anyway to obtain the Post ID from the Post Title or Post Name?

  6. prazetyo says:

    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

  7. Chris says:

    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 :)

  8. Pingback: Alex Barber | Digital Artist | WordPress get category slug from its name

  9. eas system says:

    Let me try if this will work on my weblog.

  10. I hope i ‘ll definitely get the desired results. Thanks for the tutorial mate.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>