Drupal 5: Programmatically create a CCK node

Here’s my code snippet, I use it all the time. NOTE: replace {BLAH} with your data.

<?php
// add node properties
$newNode = new StdClass;
$newNode->type = '{NODE_TYPE}';
$newNode->title = '{NODE_TITLE}'
$newNode->uid = {USER_ID};
$newNode->created = strtotime("now");
$newNode->changed = strtotime("now");
$newNode->status = 1;
$newNode->comment = 0;
$newNode->promote = 0;
$newNode->moderate = 0;
$newNode->sticky = 0;

// add CCK field data
$newNode->field_{YOUR_CUSTOM_FIELD_1}[0]['value'] = '{DATA_1}';
$newNode->field_{YOUR_CUSTOM_FIELD_2}[0]['value'] = '{DATA_2}';

// save node
node_save($newNode);
?>

NOTE: the structure of the CCK fields can vary. Here’s one way to see the structure of an already created CCK node…

<?php
$sampleNode = node_load('{ALREADY_CREATED_CCK_NODE_ID}');
echo "<PRE>" . print_r($sampleNode, TRUE) . "</pre>";
?>

Updated: