Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created a custom taxonomy "Birthday" within which i have created various dates like "December-12", "April-15"

How to get the term_id of the "December-12" by a query.

Thanks.

share|improve this question
    
You'll want to create a WP_Query and use tax_query -- If you a little more direction I can help you write the query. However I'm not sure your final uses for this so I may not be able to answer it completely without further input. –  David Mar 25 '12 at 20:27
1  
To use with the below function: $users = get_objects_in_term( $term_id, $term->taxonomy ); I want to get the $term_id ( id of "December-12" created under "Birthday") rather than using $term_id = get_queried_object_id(); Thanks. –  Vijay Mar 25 '12 at 20:31
add comment

1 Answer

You can use the get_term_by function. This allows you to obtain a term (from a given taxonomy) by its slug or name (or id). The returned term object contains the term's ID.

For example:

$term =get_term_by( 'name', 'December-12', 'Birthday');
$term_id = $term->term_id;

If 'December-12' is the slug, rather than the name, of the term, simply replace 'name' with 'slug'.

share|improve this answer
    
Thanks, i also figured out another way, i am not sure if its a clean code thought.. :-) <?php $categories = get_categories(array( 'taxonomy' => 'birthday' )); foreach( $categories as $category ) { $category_name = $category->name; $category_id = $category->cat_ID; $todays_date = date("F-d"); if ($category_name == $todays_date) { $users = get_objects_in_term( $category_id, 'birthday' ); ?> –  Vijay Mar 25 '12 at 21:19
    
That would do it - but its less efficient and my answer only takes two lines :D. Its also more complicated to read. –  Stephen Harris Mar 25 '12 at 21:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.