By Rwky


2011-08-07 14:57:22 8 Comments

I have a node and want to get all the taxonomy terms assigned to the node.

In drupal 6 I could use $node->taxonomy and the taxonomy_* functions but these don't exist in drupal 7.

2 comments

@Ronnbot 2017-03-09 21:54:33

Here's a very generic way to grab all terms without specifying field names and no db_query:

function example_get_terms($node) {
  $terms = array();

  foreach (field_info_instances('node', $node->type) as $fieldname => $info) {
    foreach (field_get_items('node', $node, $fieldname) as $item) {
      if (is_array($item) && !empty($item['tid']) && $term = taxonomy_term_load($item['tid'])) {
        $terms[] = $term->name;
      }
    }
  }
  return $terms;
}

@Sébastien Gicquel 2017-04-07 08:40:38

For Drupal beginner, i would add if (arg(0) == 'node' && is_numeric(arg(1))) {$nid = arg(1);} to get the nid and $node = node_load($nid); in order to make your function work.

@keithm 2011-08-07 15:41:17

Taxonomy terms are implemented in fields in Drupal 7. Assuming you have defined a taxonomy field named field_category for your content type, you can access it as:

$language = 'und'; // or will be provided by some Drupal hooks
foreach ($node->field_category[$language] as $delta => $value) {
  $term = taxonomy_term_load($value['tid']);
}

If you don't have access to the field name, probably the easiest way for nodes is to query the database directly:

$results = db_query('SELECT tid FROM {taxonomy_index} WHERE nid = :nid', array(':nid' => $node->nid));
foreach ($results as $result) {
  $term = taxonomy_term_load($result->tid);
}

Keep in mind though, that you may end up dealing a jumble of terms from different vocabularies if you have more than one taxonomy field.

@Rwky 2011-08-07 15:44:27

What if I don't know the taxonomy field name? The names will differ between content types and I want to get all of the terms for any node.

@keithm 2011-08-07 16:02:26

Added a method if you don't know the field name.

@Berdir 2011-11-19 11:29:26

Especially in the second version, you can use taxonomy_term_load_multiple($results->fetchCol()); to load the terms in a single query.

@Berdir 2011-11-19 11:32:36

Oh, and you can use field_get_items('node', $node, 'field_category') to get the fields in the correct language.

@Dave Cohen 2013-12-07 13:59:37

Note the select from taxonomy_index technique works only with published nodes. That table is not maintained for unpublished content.

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] How to print taxonomy terms field in node templates?

2 Answered Questions

2 Answered Questions

[SOLVED] List all terms from a vocabulary

  • 2013-03-04 11:46:57
  • LeBlaireau
  • 45729 View
  • 25 Score
  • 2 Answer
  • Tags:   7 taxonomy-terms

2 Answered Questions

[SOLVED] How to prevent taxonomy terms being duplicated on node form?

  • 2016-03-07 11:34:20
  • herci
  • 631 View
  • 1 Score
  • 2 Answer
  • Tags:   7 taxonomy-terms

1 Answered Questions

[SOLVED] Assign to node all child terms if parent term selected

2 Answered Questions

[SOLVED] How to add multiple taxonomy terms to node?

  • 2013-05-30 11:49:06
  • hakala
  • 1142 View
  • 0 Score
  • 2 Answer
  • Tags:   taxonomy-terms

1 Answered Questions

Taxonomy Terms jump list

  • 2012-10-14 15:44:57
  • woutr_be
  • 135 View
  • 0 Score
  • 1 Answer
  • Tags:   taxonomy-terms

4 Answered Questions

[SOLVED] views: all terms as images

1 Answered Questions

1 Answered Questions

[SOLVED] get taxonomy terms of parent node while in block

Sponsored Content