Its possible to add a node using the code below, but how do i add multiple fields?

$node = new StdClass();//creating a bare node
$node->type = 'answer';//giving it type
$node->status = 1;//give it a published staus
$node->title = "Step up to the FLava";//gives title
$node->body = "Body Body body"; //gives body


Singles custom field, we can use:

$node->field_my_custom_field['und'][0]['value'] = 'my value';

But what if I want to add multiple entries for one field?


Second index after 'und' is used for delta of values. You can make it 1 , 2, 3 etc to add multiple values... Something like below...

$node->field_my_custom_field['und'][0]['value'] = 'my value one';
$node->field_my_custom_field['und'][1]['value'] = 'my value two';
$node->field_my_custom_field['und'][2]['value'] = 'my value three';
$node->field_my_custom_field['und'][3]['value'] = 'my value four';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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