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.


(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);

See also PHP docs on array_pop here

  • 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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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