node_save tags by term name with auto addition of new tags

Drupal lets you add a taxonomy term to a node and if you use the GUI you can have a nice auto-complete widget that takes term name - and iof the term doesn't exist yet you get a new term created when you save the node.

I wanted to access the same functionality on node I am generating from external data.

The key is to create the term array with tid = 'autocreate' - this and the vocabulary ID are enough to trigger the taxonomy module to lookup the tid for you - and adda new term is required.

<?php
$node
= new stdClass();
$node->type='mytype';
$node-title='foo';
$node->body = 'bar';

$node->field_cat_tag['und'][0]['name'] =  $name;
$node->field_cat_tag['und'][0]['vocabulary_machine_name'] = $vocab_machine_name; // optional - helps performance if in a loop 
$node->field_g4_cat_tag['und'][0]['vid'] = $vid;
$node->field_g4_cat_tag['und'][0]['tid'] = 'autocreate';

$node_save($node);
?>

Tags

Post new comment

Got something to add - just enter a comment
all other fields are optional.

Your email address will not be published.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image without spaces, also respect upper and lower case.