4.6.x taxonomy.module taxonomy_node_get_terms_by_vocabulary($nid, $vid, $key = 'tid')
4.7.x taxonomy.module taxonomy_node_get_terms_by_vocabulary($nid, $vid, $key = 'tid')
5.x taxonomy.module taxonomy_node_get_terms_by_vocabulary($nid, $vid, $key = 'tid')
6.x taxonomy.module taxonomy_node_get_terms_by_vocabulary($node, $vid, $key = 'tid')

Find all terms associated with the given node, within one vocabulary.

1 call to taxonomy_node_get_terms_by_vocabulary()
forum_form in modules/forum/forum.module
Implementation of hook_form().


modules/taxonomy/taxonomy.module, line 620
Enables the organization of content into categories.


function taxonomy_node_get_terms_by_vocabulary($node, $vid, $key = 'tid') {
  $result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.vid = %d ORDER BY weight', 't', 'tid'), $vid, $node->vid);
  $terms = array();
  while ($term = db_fetch_object($result)) {
    $terms[$term->{$key}] = $term;
  return $terms;


Ron.Brash’s picture

If implemented in something like node-yourcontent.tpl.php and you have two vocabularies

                    $free = taxonomy_node_get_terms_by_vocabulary($node, 2); // vid=2, Vocab FREEE
                    $signup = taxonomy_node_get_terms_by_vocabulary($node, 3); // vid=3, Vocab NOT FREE
                    if(!empty($free)) {
                        echo ' no need to signup!';
                    elseif(!empty($signup)) {
                        echo 'signup soldier!';
                    else {
                        echo 'we are sorry your call cannot be completed as dialed.  Please hangup and try again.';
kiamlaluno’s picture

taxonomy_node_get_terms_by_vocabulary() is not a hook; it's a normal function.

drclaw’s picture

It's helps to know that this function returns an array of stdClass objects with properties named after the database fields. Also, in Drupal 6, this function takes a node object reference, not a nid (like in D5).

// Let's just pretend we know the node id and vocabulary id for now...
$nid = 333;
$vid = 4;
// Load the Node Object using Drupal's built in node_load()
$node = node_load($nid);
$terms = taxonomy_node_get_terms_by_vocabulary($node, $vid);
// To store the new terms (if necessary...)
$new_terms = array();
if ($terms) {
    foreach ($terms as $term) {
        // Each term has the properties:
        // $term->tid
        // $term->vid
        // $term->name
        // $term->description
        // $term->weight
        // To just print the results use something like: 
        print $term->name . '<br />';
        // Or create an array of just the term names:
        $new_terms[] = $term->name;
cosmogenesis’s picture

  $terms = array();
  if (isset($node->taxonomy)) {
    foreach ($node->taxonomy as $voc) {
      if ($voc->vid == $vid) {
        $terms[] = $voc;
  } else {
    // check db in case node doesn't have terms
    $terms = taxonomy_node_get_terms_by_vocabulary($node, $vid);    

  return $terms;
agerson’s picture

function taxonomy_node_get_terms_by_vocabulary($node, $vid, $key = 'tid') {
  $result = db_query('SELECT t.tid, t.* FROM {taxonomy_term_data} t INNER JOIN {taxonomy_index} r ON r.tid = t.tid WHERE t.vid = :vid AND r.nid = :node_nid ORDER BY weight', array(':vid' => $vid, ':node_nid' => $node->nid));
  return $result->fetchAll();
johnennew’s picture

The Drupal 7 version posted above does not work when the node has been unpublished as D7 clears entries from the taxonomy_index table when node is unpublished. The actual data is stored in fields now in D7 which probably means your D6 solution needs re architecting for D7.