taxonomy_node_get_terms_by_vocabulary

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

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

2 calls to taxonomy_node_get_terms_by_vocabulary()

File

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

Code

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;
}

Comments

Example using this hook

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

<?php
                    $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.';
                    }
                   
?>

---

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

Another Example

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).

<?php
// 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;
    }
}
?>

Should check for values in the node before hitting the db.

<?php
  $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;
?>

Drupal 7 version

<?php
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();
}
?>

Drupal 7 Version does not work with unpublished content

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.

Login or register to post comments