Copy taxonomy terms from one node to another
Particularly un-clever code:
function wsf_action_copy_terms($from_nid, $to_nid) {
// can you spell optimizable? Neither can we.
// this whole thing could be done with a single mysql query probably
// but would that be the Drupal way?
// probably would've been better to rip out the guts here... http://api.drupal.org/api/function/taxonomy_node_get_terms/5
$terms = taxonomy_node_get_terms($from_nid);
// slower but more specific if it ever matters: $terms = taxonomy_node_get_terms_by_vocabulary($from_nid, place_taxonomy_get_vid());
// adapted from http://api.drupal.org/api/function/taxonomy_node_save/5
// WARNING: INSERT IGNORE is MySQL only
foreach ($terms as $tid => $term) {
db_query('INSERT IGNORE INTO {term_node} (nid, tid) VALUES (%d, %d)', $to_nid, $tid);
}
}
For more, see:
http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html
Comments
Post new comment