I have the following content types: hotels, and taxonomy terms; in hotels I have a taxonomy field.

How do I retrieve the taxonomy term ID in a node? Is it possible to retrieve the self and parent taxonomy term ID and name?

I have also tried this, but it does not work.


You can use field_get_items(), which returns you the value of a field in the language they would be displayed, or in the language whose code is passed as argument to the function. Generally, you are interested to the field's value in the language that would be used to show the field, and you don't pass any language code to the function.

$items = field_get_items('node', $node, $field_name);

$node contains the node object, and $field_name the name of the taxonomy term field.

What returned in $items is an array containing (possibly) multiple values; each value is an array containing the "tid" index, and the term ID. For a taxonomy term for which users can enter more than a value, this is what the function returns.


| improve this answer | |

You can use field_info_instance(), as in the following snippet.

$info = field_info_instance('node','enter_your_field_name','enter_content_type_name');
foreach ($info as $ps2 => $key) {
  echo "Info [ $ps2 ] : " . $key . "<br />";

In this way, you get the term ID.

| improve this answer | |
  • field_info_instance() is not returning any term ID. If you have two different nodes, with two different term IDs, that function would return the same value, as it doesn't ask you to which node you are referring. The ID it is returning is a different ID. – kiamlaluno May 21 '12 at 8:57
  • @kiamlaluno , then how do we get Term id in a node – Rafee May 21 '12 at 9:15

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.