Hi,

Is there any way to get the node vocabulary IDs it blongs to?

(Not term id tid, vocabulary id)

Thanks,
Leandro

Comments

nevets’s picture

Given either a node id (nid) you could do something like

<?php
// Assume we have a nid as $nid

$sql = "SELECT DISTINCT(vid) FROM {term_node} tn JOIN {term_data} td USING(tid) WHERE tn.nid = %d";
$results = db_query($sql, $nid);
while ( $data = db_fetch_object($results) ) {
  // Do something with the vid, here we just print it
  print $data->vid;
}
?>

Given a node you could do something like

<?php
// Code assumes node is in $node
$vids = array();
foreach ( (array)$node->taxonomy as $term ) {
  $vids[$term->vid] = $term->vid;
}
if ( !empty($vids) ) {
 // Do something with the vids, here we print them
 print implode(', ', $vids);
}
?>
Leech’s picture

Awesome, thank you!

Both solutions are good to keep in mind!