hook_form_alter selecting a default value in a node reference select list I am trying to add some logic to auto select a node reference option on a select list on my content type. I am trying to set it with the following.

$form['field_my_field']['#default_value'][0]['nid'] = 5;


Where 5 is a nid. The value is set correctly in the form but when it is rendered to the page the option in the select list is not selected.

What am I doing wrong?
CCK requires a little voodoo to alter values with a hook_form_alter(). You need to implement a special type: after_build, so put this code in a custom.module:

//Declare the callback for after_build for page node form
function custom_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  if ($form_id == 'page_node_form) {
    $form['#after_build'][] = 'custom_after_build';
  }
}

function custom_after_build($form, &amp;$form_state) {
  //Set my node ref to 5
  $form['field_node']['nid']['nid']['#value'][0] = 5;
  //you can set more than one using 
  $form['field_node']['nid']['nid']['#value'][2] = 6;
  //have a look at the form after after_build
  dsm($form);
  return $form;
}


I adapted a previous answer of mine, but I have to admin that node reference is even more confusing ;)