Create drupal node programatically and set custom select list field I have a Drupal 7 node with custom fields. I have no problem setting values to the single text fields but I'm not sure how to set my select list field to one of the allowed keys of the field.

This is the code I've tried to assign the select list to the 'pending' key.

$node-&gt;field_referee_status['und'][0]['tid'] = 'pending';


Node creation code:

// CREATE NODE
$node = new stdClass;
$node-&gt;type = 'referee';
$node-&gt;title = 'Referee report for xx';
$node-&gt;uid = $user-&gt;uid;
$node-&gt;status = FALSE;
$node-&gt;field_referee_status['und'][0]['tid'] = 'pending';
$node-&gt;field_referee_id['und'][0]['value'] = 'test';
$node-&gt;field_testff['und'][0]['value'] = "testworked";
node_object_prepare($node);
node_save($node);


Question: Does anyone know the correct way to assign the key value to a custom select list field when creating Drupal nodes programmatically?
With a select list, you need to store the key value of the custom field.

I just noticed that you're storing taxonomy term reference value, which means you should be storing the term ID and not the actual text value of pending.

Theres a handy function which allows you to grab the TID based on a term name:

$term = taxonomy_get_term_by_name('pending');
$node-&gt;field_referee_status[LANGUAGE_NONE][0]['tid'] = $term-&gt;tid;


See the docs here.

EDIT

To save into a normal textfield select list:

$node-&gt;field_referee_status[LANGUAGE_NONE][0]['value'] = 'pending';