Creating taxonomy terms via the Drupal API

It took me a while to find reference for this but here goes. To create a taxonomy term in a module for a custom node module you need to do something like the following into the _insert function

$vid = variable_get('node_product_vocabulary', '');   // vocabulary ID is hard coded for this example
$autoterm = array(
'name' => $node->title, // or whatever you want the auto-term to be named
'parent' => 0,
'vid' => $vid,
);
taxonomy_save_term($autoterm);
$tid = (int) db_result(db_query('SELECT MAX(tid) FROM {term_data} WHERE vid = %d', $vid));
$node->taxonomy[$vid][$tid] = $tid;
This reference was taken from the Drupal site here
 

Your rating: None Average: 3 (2 votes)