4

I am trying to build a query that finds all nodes of a custom type where one of the fields is a user Entity Reference. I am trying to use EntityFieldQuery but just as open to db_select(). The following is just an example of one of the queries I have tried.

$query = new EntityFieldQuery();

$query
  ->entityCondition('entity_type', 'node', '=')
  ->propertyCondition('type', 'custom_node', '=')
  ->fieldCondition('field_marketer', 'value', $user->uid, '=');

$entities = $query->execute();

Thanks in advance for any help.

  • Wouldn't that be 'field_user_entity_reference'? – Clive Feb 7 '13 at 20:16
  • The actual field name is 'field_marketer'. – user320691 Feb 7 '13 at 20:22
8

The column name for an entity reference field is target_id, so your code should be:

$query = new EntityFieldQuery();

$query
  ->entityCondition('entity_type', 'node', '=')
  ->propertyCondition('type', 'custom_node', '=')
  ->fieldCondition('field_marketer', 'target_id', $user->uid, '=');

$entities = $query->execute();
  • That's it! Thank you again Clive! This was my first attempt at using EntityFieldQuery. – user320691 Feb 7 '13 at 21:01

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.