I have two content type A and B in which B content type node is reference in A content type node. In B content type node there is status field. I want to fetch the A content type node in which the status of B content type node is something.

Can you please guide me how can I use this?



1 Answer 1


This is the best solution for you ==> https://www.drupal.org/node/1343708

EFQ an beautifull tools for manipulate the entities in drupal.

Do not hesitate if you need precision but this doc is verry good.

In the Entity Reference case

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

$query = new EntityFieldQuery();

  ->entityCondition('entity_type', 'node', '=')
  ->propertyCondition('type', 'custom_node_type', '=')
  ->fieldCondition('field_reference_name', 'target_id', $id, '=');

$entities = $query->execute();

For follow the relation of your entities you need to reference the A into B or B into A with a reference field. When you use EFQ to search the relation in node A with B you need to use fieldCondition in this reference field in my example is field_reference_name. If you list the content B (ids) into an array and you user this

$query = new EntityFieldQuery();

  ->entityCondition('entity_type', 'node', '=')
  ->propertyCondition('type', 'a', '=')

$entities_a = $query->execute();

For search the relation A to B into A you need to use this :

$query = new EntityFieldQuery();

  ->entityCondition('entity_type', 'node', '=')
  ->propertyCondition('type', 'b', '=')
  ->fieldCondition('field_reference', 'target_id', $entities_a, 'IN');

$entities_b_refered_into_a = $query->execute();

Good chance.

  • From where I get the ID Variable in your query . This was my query : $query ->entityCondition('entity_type', 'node') ->entityCondition('bundle', array('candidate','interview'), 'IN') ->propertyCondition('status', 1); $query->fieldCondition('field_interview_status', 'value', 'new', 'IN'); // This field is in interview content type with status dropdown $result = $query->execute(); This gives me the B content type nodes. But I need A content type node. Commented Sep 1, 2015 at 11:58
  • EFQ operates on one entity/bundle type at a time, it can't follow relationships nor extend queries based on related entities' properties @Leo
    – Clive
    Commented Sep 1, 2015 at 12:10
  • The propertyCondition in EFQ support multiple args ex : $name = ['a', 'b', 'c']; $query = new EntityFieldQuery(); $query->entityCondition('entity_type', 'node') ->propertyCondition('type', '$name', 'IN'); $result = $query->execute();
    – woprrr
    Commented Sep 2, 2015 at 7:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.