I'm trying to use PHP to get user IDs from an Entity Reference of users in a content type.

I have got as far as doing (where I actually get the NID from elsewhere):

$node = node_load(123);

$info = field_get_items('node', $node, 'field_my_users');

But how do I get the IDs from $info?


You just need to get the target_id from each of the field values, which you can do by looping through $info:

$uids = array();
foreach ($info as $field_item) {
  // Entity reference fields store their data in the 'target_id' column
  $uids[] = $field_item['target_id'];
  • Hi @Clive, yes %node instead of $node was just a typo in my question - shift+5 vs shift+4 - edited my Q and your A accordingly. I tested this by doing print (string)$field_item['target_id']; and nothing was printed. However, checking count($info) confirms the data is in there. Any ideas? – dav_i Oct 22 '12 at 8:32
  • Could you post a print_r of the output of $info in your question? – Clive Oct 22 '12 at 8:40
  • Agh - I'm an idiot - I had a spelling mistake in my field_get_items(. Your solution works perfectly. Thankyou. – dav_i Oct 22 '12 at 8:56

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.