<?php
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
module_load_include('inc', 'node', 'node.pages');
$node = new stdClass();
//Set up default values, if required.
node_object_prepare($node);
//Specify the content type
$node->type = 'profile';
//Specify an author for the node
$node->uid = 1;
//Add the title
$node->title = 'Test';
//Add the CCK fields data
$node->field_name[0]['value'] = 'Name';
$node->field_surname[0]['value'] = 'Surname';
//Save the node object into the database.
node_save($node);
//Debug the created node
print '<pre>';
print_($node);
print '</pre>';
-
Useful post!
A question though, is there a way to programmatically create a node and have it automatically include all cck fields defined for that node type? I have a node type with a lot of checkboxes that can change from time to time and I don’t want to have to make code changes every time an administrator wants to add new fields?
Cheers!

1 comment
Comments feed for this article
Trackback link: http://lucasforge.bmeme.com/2010/06/drupal-6-create-a-node-with-cck-fields-programmatically/trackback/