Skip to content

Instantly share code, notes, and snippets.

@jcisio
Created January 27, 2013 11:16
Show Gist options
  • Save jcisio/4647905 to your computer and use it in GitHub Desktop.
Save jcisio/4647905 to your computer and use it in GitHub Desktop.
Move taxonomy from node to content taxonomy fields. Use with drush.
<?php
/**
* @file
* Move taxonomy term from taxonomy_node table to field tables.
*/
$types = array(
array(
'type' => 'product',
'fields' => array(
'field_product_keyword' => 3,
'field_product_company' => 11,
),
),
);
foreach ($types as $type) {
update_nodes($type);
}
function update_nodes($info) {
$count = 0;
$vids = array_values($info['fields']);
$result = db_query("SELECT nid FROM {node} WHERE type = '%s'", $info['type']);
while ($row = db_fetch_object($result)) {
$node = node_load($row->nid);
$tids = array();
$update = FALSE;
foreach ($node->taxonomy as $delta => $term) {
if (in_array($term->vid, $vids)) {
$tids[$term->vid][] = array('value' => $term->tid);
$node->taxonomy[$delta] = NULL;
$update = TRUE;
}
}
foreach ($info['fields'] as $field_name => $vid) {
if (!empty($tids[$vid])) {
$node->$field_name = $tids[$vid];
}
}
if ($update) {
node_save($node);
$count++;
}
}
printf("%d '%s' nodes have been updated.\n", $count, $info['type']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment