Skip to content

Instantly share code, notes, and snippets.

@joakim
Created October 16, 2013 10:05
Show Gist options
  • Save joakim/7005513 to your computer and use it in GitHub Desktop.
Save joakim/7005513 to your computer and use it in GitHub Desktop.
Drupal: Programmatically remove a term from multiple nodes
// ID of taxonomy term to remove.
$tid = 5;
// Name of the taxonomy field where the term is found.
$field_name = 'field_NAME';
_taxonomy_term_remove_from_nodes($tid, $field_name);
function _taxonomy_term_remove_from_nodes($tid, $field_name) {
$nids = taxonomy_select_nodes($tid, FALSE, FALSE);
$nodes = node_load_multiple($nids);
if (empty($nodes)) return;
foreach ($nodes as $node) {
$field = &$node->$field_name[LANGUAGE_NONE];
foreach ($field as $key => $term) {
if ($term['tid'] == $tid) {
unset($field[$key]);
node_save($node);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment