WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I know the current post id that i have in the variable $pid

I use $terms = get_the_terms($pid, 'custom_category');

How do I get just the term id/term_taxonomy_id

if I var_dump $terms I see what I want...but I have no idea how the heck to return just the id, not an array, just the id.

Bare in mind...I less than 1/2 know what I'm doing...just face rolling keyboard to get what I want...but learning more everyday.



share|improve this question
up vote 2 down vote accepted

(Probably better to use get_the_terms).

$terms =  wp_get_object_terms( $pid, 'custom_category', array('fields'=>'ids'))

Get an array of term ids (will always been array, even if it is an array of one):

$ids = wp_list_pluck( $terms, 'term_id' );

If you just want one id... then 'pop' out the last id:

 $id = array_pop($ids)
share|improve this answer
appreciate the answer...but that still just returns an array that I don't know how to get the id out of...I'm very much a rookie...no idea what to do with the return. – Brian Thornton Apr 25 '12 at 7:13
@BrianThornton have edited my answer.... – Rajeev Vyas Apr 25 '12 at 7:15
getting there...but it's late and I can barely see...will pick up again tomorrow...thanks for the help. – Brian Thornton Apr 25 '12 at 8:23
@BrianThornton - you will always have an array because posts can have more than one term associated with them. You'll have to take out the ID youself. I would use get_the_terms rather than wp_get_object_terms. – Stephen Harris Apr 25 '12 at 8:53
So this answered my question....the basics are going to be the death of me. I'm still not able to get what I want/need to work...but it's involved. So I'm writing up a rather lengthy question that I will post in a little while. Thanks guys...I learn more every day here! – Brian Thornton Apr 25 '12 at 19:24

try this code for getting category id from post id.

$categories = get_the_category( $post_id );
share|improve this answer
Except for the fact that from codex.wordpress.org/Function_Reference/get_the_category "This function only returns results from the default "category" taxonomy. For custom taxonomies use get_the_terms." Believe me...I wish I could use it.... – Brian Thornton Apr 25 '12 at 8:23

Your Answer


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.