serundeputy's picture

How does one save a taxonomy term to a node programmatically?

When creating a node programmatically that has a taxonomy term field how do you save the term reference field?

Accepted answer

  // Make a post node.                                                          
  $node = new Node();                                                            
 
  // Use the node type you need here, in this case 'post'.                                               
  $node->type = $type;
  $node->title = 'Replace This With The Title You Want';                                                          
  node_object_prepare($node);                                                    
  $node->language = LANGUAGE_NONE;                                               
  $node->uid = 1;                                                                
  $node->status = 1;                                                             
  $node->promote = 0;                                                            
  $node->comment = 0;
 
  // Save your field data.
  $node->body[$node->language][]['value'] = 'geoff is the best.';
  $node->field_author[$node->language][]['value'] = 'geoff';
  $node->field_date[$node->language][]['value'] = date('now');
 
  // Save a taxonomy term field.
  $node->field_tags[$node->language][]['tid'] = 1;

  // Save submit and save the node.
  $node = node_submit($node);                                                    
  node_save($node);

 

the most relevant part being:

// Save a taxonomy term field.
$node->field_tags[$node->language][]['tid'] = 1;

the taxonomy field does not save a 'value' like the other fields but rather a 'tid', you will need to now the `tid` of the term you wish to save.

Comments

  // Make a post node.                                                          
  $node = new Node();                                                            
 
  // Use the node type you need here, in this case 'post'.                                               
  $node->type = $type;
  $node->title = 'Replace This With The Title You Want';                                                          
  node_object_prepare($node);                                                    
  $node->language = LANGUAGE_NONE;                                               
  $node->uid = 1;                                                                
  $node->status = 1;                                                             
  $node->promote = 0;                                                            
  $node->comment = 0;
 
  // Save your field data.
  $node->body[$node->language][]['value'] = 'geoff is the best.';
  $node->field_author[$node->language][]['value'] = 'geoff';
  $node->field_date[$node->language][]['value'] = date('now');
 
  // Save a taxonomy term field.
  $node->field_tags[$node->language][]['tid'] = 1;

  // Save submit and save the node.
  $node = node_submit($node);                                                    
  node_save($node);

 

the most relevant part being:

// Save a taxonomy term field.
$node->field_tags[$node->language][]['tid'] = 1;

the taxonomy field does not save a 'value' like the other fields but rather a 'tid', you will need to now the `tid` of the term you wish to save.