How does one create a node programmatically?
I would like to create nodes in code like: https://www.drupal.org/docs/7/howtos/create-a-node-in-code
How does one create a node programmatically?
I would like to create nodes in code like: https://www.drupal.org/docs/7/howtos/create-a-node-in-code
Here is how I do it:
// Make a post node. $node = new Node(); // Use the node type you need here, in this case 'post'. $node->type = 'post'; $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->field_author[$node->language][]['value'] = getMetaAndClean($thisBlog[8]); $node->field_date[$node->language][]['value'] = getMetaAndClean($thisBlog[13]); $node->field_byline[$node->language][]['value'] = getMetaAndClean($thisBlog[12]); // Save submit and save the node. $node = node_submit($node); node_save($node);
Body field is missing from example, but pretty straightforward (after banging my head against wall for an hour):
$node->body['und'][]['value'] = 'This is my test content.';
Now, I'm stuck on adding a taxonomy term to the node using default "tags" (standard installation profile). Any help?
I thought this would work, but it does not:
$node->field_animals_tags['und'][]['taxonomy_term']->name = 'Mammals';
Here is how I do it: