get category ID from Slug
-
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.
-
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”;
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.
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?
You can also loop through the categories like this:
foreach(get_categories() as $category){ if($category->slug == $THE_SLUG){ $THE_ID = $category->cat_ID; } }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.
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.
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!
No problem… I just tend to avoid raw database queries in case the database structure changed in future WP releases.
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*
good luck with that π
WordPress has a get_cat_id() function. See this page for an example: http://curtishenson.com/get-the-category-id-in-wordpress/
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;π
Yeah and you probably just grabbed that answer from one of the files in wp-include too. Like, um…. category.php ?
π
Well.. of course. Where else would I get it from?
I refer to the WordPress source to answer most of the questions here. π
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.
The topic ‘get category ID from Slug’ is closed to new replies.