UPDATE: I see that most likely the best way to accomplish this is to grab the term id from $content['field_tags']. However I'm not sure how to do that. Whenever I retrieve $content['field_tags'][value] all that gets returned is an array object. And when I attempt to render it all I get is the tag name.

I'm attempting to grab the taxonomy term id in the node template for a content type. I'm grabbing the taxonomy term by referencing the field that contains the taxonomy terms. Like so

$tag = render($content['field_tags'][$i]);

I then use taxonomy_get_term_by_name in order to grab the id for that term like so.

$term_array = taxonomy_get_term_by_name(render($content['field_tags'][$j]));
$term = reset($term_array);
$term_id = $term->tid;

It works like a charm except for one issue. I have a taxonomy category for tags as well as events, and when there is a term with the same name in both the 'event' and 'tags' category it grabs the event term id instead of from the 'tags' category, which I want.

Is there another way to grab the specific term id for the tag field? Would it be possible to grab the term id directly from this object?


1 Answer 1


Yes, you should pull the term ID directly from the field value rather than first rendering it to a term name, then trying to look up the term based on the name.

  • I understand that that would be ideal, but part of my question is how to pull the term id directly from the field value.
    – Amy
    Aug 10, 2015 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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