I've tried the technique described here: http://lucasforge.2bopen.org/2010/06/drupal-6-create-a-node-with-cck-fields-programmatically/

and here: http://drupal.org/node/1111514

Neither seem to work. The node gets created, but the CCK field I'm trying to set never gets set.

Both those just describe using "field_name[0]['value'] = 'something'" but even if I set the field value through the web form and DSM the result it never has 'value' set, just '#default_value'.

I tried setting "field_name[0]['#default_value'][0]['nid'] = 'mynodeid'" but that didn't work either.

This is the structure of the CCK field I'm trying to set:

field_application_discipline_id (Array, 13 elements)

#type (String, 20 characters ) nodereference_select
#default_value (Array, 1 element)
#required (String, 1 characters ) 0
#columns (Array, 1 element)
#title (String, 13 characters ) Discipline ID
#description (String, 0 characters )
#delta (Integer) 0
#field_name (String, 31 characters ) field_application_discipline_id
#type_name (String, 11 characters ) application
#tree (Boolean) TRUE
#weight (String, 2 characters ) 31
#access (Boolean) TRUE
#count (Integer) 9

Any idea why this wouldn't be working?

1 Answer 1


For nodereference fields you have to use "nid" instead of "value".

//123 is the nid of the node you want to reference 
$node->field_application_discipline_id[0]['nid'] = 123;

The best way to discover what variable to use is to install Devel module and view the devel tab next to the view/edit tab on the node page.

  • This worked! Thanks for the devel tip, I never realized what that tab was for.
    – Joren
    Commented Aug 1, 2011 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.