Saturday, September 19, 2009

Programmatically create a CCK node

Programmatically create a CCK node
Submitted by Eric on Mon, 04/14/2008 - 11:20

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 = (object) NULL;
$newNode->type = '{NODE_TYPE}';
$newNode->language = '';
$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 "
" . print_r($sampleNode, TRUE) . "
";

No comments: