WordPress.org

Ready to get started?Download WordPress

Forums

get category ID from Slug (21 posts)

  1. Ivovic
    Member
    Posted 6 years ago #

    I want to automate a certain functionality based on the categories that I add.

    If I have the category slug, how can I get it's category ID?

    Thanks guys.

  2. Michael Torbert
    WordPress Virtuoso
    Posted 6 years ago #

    I didn't bother to look up the exact terms, but it would be something like this:

    $query="SELECT cat_id FROM $wpdb->categories WHERE category_nicename = $your_slug";

  3. Ivovic
    Member
    Posted 6 years ago #

    I was hoping to use an internal function to maybe take advantage of a previously run query if possible... but failing that, this'll work out great.

    Thank you.

  4. Ivovic
    Member
    Posted 6 years ago #

    Still looking for an already populated variable, or some other way to avoid a query, but ... in the mean time:

    $id = $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE slug='$myslug'");

    does the trick.

    I figure I may as well do this unless someone has a better idea?

  5. wp_guy
    Member
    Posted 6 years ago #

    You can also loop through the categories like this:

    foreach(get_categories() as $category){
        if($category->slug == $THE_SLUG){
            $THE_ID = $category->cat_ID;
        }
    }
  6. Ivovic
    Member
    Posted 6 years ago #

    cheers for that wp_guy.

    EDIT: I just realised you were talking about get_categories (not get_the_category) which can be used outside the loop, in which case your post may actually prove very useful, although I'm sure get_categories uses a query too. Excuse my mistake.

    ...

    I wasn't very specific in my opening post though, but let me fill you in.

    I'm not doing this in the loop - I'm going to assemble a page based on a user profile field, so... if you enter YourCompany in the profile field, and your user level is sufficient, you'll be able to view a page which pulls posts from various categories with slugs such as 'yourcompany', 'yourcompany-tickets', 'yourcompany-news' etc.

    so I will have 'YourCompany' as the user profile field, but now I need to pull the category IDs for each of those categories I mentioned earlier -- outside the loop of course... to be stuffed into various areas like the main content and the sidebar as well.

    Basically I'm trying to be a tricky bastard, to avoid having to modify templates each time there's a new company available. I'll just make the categories, and the page will work.

    thanks for adding your 2cents though - these threads are useful forever.

  7. Ivovic
    Member
    Posted 6 years ago #

    yep, as I suspected, this isn't something that uses variables already pulled by other queries, so get_categories adds a query, but then so does wp_list_categories in the sidebar.

    I may be just picky enough to do this once, then build my own category list from a global var in the sidebar... we'll see.

    thanks for the suggestion though - it's another viable option.

  8. Ivovic
    Member
    Posted 6 years ago #

    As it turns out... I'm now pulling two category IDs each time, so it makes more sense to use get_categories() and loop through them, as I'd have to loop through either way.

    thanks wp_guy!

  9. wp_guy
    Member
    Posted 6 years ago #

    No problem... I just tend to avoid raw database queries in case the database structure changed in future WP releases.

  10. Ivovic
    Member
    Posted 6 years ago #

    yep - with my luck though, half the undocumented functions I'm using to play with the user metadata will change in the next version. *sigh*

  11. wp_guy
    Member
    Posted 6 years ago #

    good luck with that ;-)

  12. bypan6
    Member
    Posted 6 years ago #

    WordPress has a get_cat_id() function. See this page for an example: http://curtishenson.com/get-the-category-id-in-wordpress/

  13. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    If I have the category slug, how can I get it's category ID?

    Wow. I can't believe nobody got this one right.

    $category = get_category_by_slug('slug');
    echo $category->term_id;

    :)

  14. Roger Theriault
    Member
    Posted 6 years ago #

    Yeah and you probably just grabbed that answer from one of the files in wp-include too. Like, um.... category.php ?

    :-)

  15. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Well.. of course. Where else would I get it from?

    I refer to the WordPress source to answer most of the questions here. :)

  16. Roger Theriault
    Member
    Posted 6 years ago #

    It would be the best reference, it's hard for the code to contradict the docs (unless it's a bug). And that particular file is quite well commented. Plus, not every function or CSS class is documented in the Codex.

  17. uxte
    Member
    Posted 5 years ago #

    bypan6 had the right answer though...

    $catid = get_cat_id('slug');
    echo $catid;

  18. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    No, the get_cat_ID function gets a category by the name of the category, not by the slug.

  19. uxte
    Member
    Posted 5 years ago #

    I have just tried and it worked ;D

  20. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    Only because you have the same slug as the category name is. If the slug had been different (like if you have a space in your category name, it'll be a dash in the slug), then your code won't work.

  21. Dan Cameron
    Member
    Posted 5 years ago #

    Pretty old topic but this might be useful:
    get_category_by_slug('category-slug')->term_id

    I'm using it in:
    <?php echo category_description(get_category_by_slug('category-slug')->term_id); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic