I am trying to finalize migration from d6 content profile to d7. I'd like to execute an arbirary php script using VBO to set a multi value entity reference field(users) based on the authors of a multi value node reference(bio content type) field.

I am looking for proper syntax for looping through each node reference item and populating the entity(user) reference with the uid from that node.

Final working solution:

$containing_node = node_load($entity->nid); //$nid is what node if you get from VBO
$w_containing_node = entity_metadata_wrapper('node', $containing_node);
$references = field_get_items('node', $containing_node, 'field_res_mem');
   foreach($references as $index => $value) {
     $my_index = 0;
     $reference_nid = $value['nid'];
     $rnode = node_load($reference_nid);
    $ruid = $rnode->uid;
    $w_containing_node->field_res_memuser[] = $ruid;

1 Answer 1


For any field that has multiple values you can use field_get_items() to retrieve all values. For your specific case with the entity reference field, you'll need to retrieve the target_id property from each field item.

  $node = node_load($nid); //$nid is the node id you get from VBO
  $references = field_get_items('node', $node, 'field_MY_REFERENCE_FIELD'); 
  foreach($references as $index => $value) {
    $reference_nid = $value['target_id'];
    //@TODO: use $reference_nid to retrieve anything about referenced node
  • Thank you this got me going toward a final working solution. I appreciated it. Sep 21, 2015 at 18:17

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.