4

I want to update some nodes in my system and update a particular taxonomy attach to them. I need to do this work programmatically. I have node ids and term id and want to attach this term to this particular node. What should do the trick here??

Thanks

1 Answer 1

6

To get the node:

$node = node_load($X); // $X is node ID.

To attache a term ID - note that this is very different in D7:

$node->taxonomy[$tid] = taxonomy_get_term($tid); // $tid is attaching term ID.

To add revision:

$node->revision = 1; 
$node->log = "Programatically attached taxonomy term.";

To save the node - if the node is new:

node_submit($node);
node_save($node);

or, to save the node if it's an existing node:

node_save($node);

In Drupal 7, terms are attached as normal field items so field_first_name[0] means the first occurrence of the field. Don't confuse that functionality with terms. You can add as many as terms you want.

Update: Thanks to kliker for the edit, which was unfortunately rejected. It seems I have to edit the post myself to get his changed into this answer.

Not the answer you're looking for? Browse other questions tagged or ask your own question.