How to set a node reference programmatically? I'm creating a new node and attempting to the values of the fields programmatically. I cannot seem to figure out how to set a value for a node reference field, the widget type is autocomplete and it is set to unlimited values. 

Here is my code to create the new node and an entity metadata wrapper

// Create the communication node and set the current user as the author
$new_comm = entity_create('node', array('type' =&gt; 'comm', 'uid' =&gt; $uid));
// create a wrapper for the new communication
$nc_wrapper = entity_metadata_wrapper('node', $new_comm);
// then I set some other field values via the $nc_wrapper


I cannot seem to set the value for the node reference field field_comm_relationship. I have tried all manner of setting the value including the following ($oc_wrapper wraps a different node type that also has a node reference):

I tried inserting it straight from the $oc_wrapper

$nc_wrapper-&gt;field_comm_relationship = $oc_wrapper-&gt;field_call_site_ref-&gt;value();


I tried using the set method

$nc_wrapper-&gt;field_comm_relationship-&gt;set($oc_wrapper-&gt;field_call_site_ref-&gt;value());


I tried both of the previous while accessing the actual nid value

$nc_wrapper-&gt;field_comm_relationship-&gt;set($oc_wrapper-&gt;field_call_site_ref-&gt;value()-&gt;nid);


and

$nc_wrapper-&gt;field_comm_relationship = $oc_wrapper-&gt;field_call_site_ref-&gt;value()-&gt;nid;


I tried to make the input an array 

$site_nid = ['und' =&gt; [ 0 =&gt; ['nid' =&gt; (int) $oc_wrapper-&gt;field_call_site_ref-&gt;value()-&gt;nid]]];
$nc_wrapper-&gt;field_comm_relationship = $site_nid;


I tried using the node_load function instead of EMW

$node = node_load('node', $nc_wrapper-&gt;nid-&gt;value());
$site_nid = $oc_wrapper-&gt;field_call_site_ref-&gt;value()-&gt;nid;
$node-&gt;field_comm_relationship['und'][0]['nid'] = $site_nid;
node_save($node);


As suggested I do call save() later in my code, but it's not making it there because it hits a fatal error. For instance if I run this code

$nc_wrapper-&gt;field_comm_relationship = (int) $oc_wrapper-&gt;field_call_site_ref-&gt;value()-&gt;nid;


I get this error

EntityMetadataWrapperException: Invalid data value given. 
Be sure it matches the required data type and format. 
Value at node()-&gt;field_comm_relationship: 713511. in EntityMetadataWrapper-&gt;set() 
(line 122 of /vagrant/sites/all/modules/contrib/entity/includes/entity.wrapper.inc)
After updating the value in your entity metadata wrapper you need to be sure to also call the save() method to save your change. See this documentation on drupal.org.

Since you have an object, and not an id, the following should work.

// Set value for a single value field.
$nc_wrapper-&gt;field_comm_relationship = $oc_wrapper-&gt;field_call_site_ref-&gt;value();


or

// Set value for a multivalue field. Make sure to pass an array of objects.
$nc_wrapper-&gt;field_comm_relationship = [$oc_wrapper-&gt;field_call_site_ref-&gt;value()];


or

// Append an additional value to a multivalue field.
$nc_wrapper-&gt;field_comm_relationship[] = $oc_wrapper-&gt;field_call_site_ref-&gt;value();


and finally

// Save the host entity.
$nc_wrapper-&gt;save();


If you have ids, check out this answer. You can get an id instead of an object from the entity metadata wrapper like $oc_wrapper-&gt;field_call_site_ref-&gt;raw() instead of -&gt;value();

Additional info for entity metadata wrappers can be found in this Death to Field Arrays! article.