Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the feedzy-rss-feeds domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /data01/virt126885/domeenid/www.saksadogi.com/codegurus.eu/wp-includes/functions.php on line 6121
Adding content programmatically in Drupal 7 - CodeGurus

Adding content programmatically in Drupal 7

Sometimes you really need to add content programmatically and here’s a little sample about the basics.

$node = new stdClass(); 

// Set content type
$node->type = 'article';

// Prepare defaults
node_object_prepare($node);

// Define language (currently language neutral)
$node->language = LANGUAGE_NONE;

// Basic content
$node->title = 'Test';
$node->body[$node->language][0]['value'] = 'Body text';

// Example if using custom fields
$node->field_CUSTOM_FIELD[$node->language][0]['value'] = 'Value';
// Example if using fields that are taxonomy type
$node->field_CUSTOM_FIELD_TAXONOMY[$node->language][0]['tid'] = 1;

// Save node
node_save($node);

For updating nodes load them first with:

// Node id you want to update
$nid = 1;
$node = node_load($nid);

And if you want to use revisions add the following:

$node->revision = 1;
$node->log = 'Node updated at ' . date('F j, Y, G:i');

Posted

in

by

Comments

Leave a Reply