How to query entities/nodes that does not have the field "field_parent" with EntityFieldQuery? I'm using the Entity reference module and want to select all nodes that has no parent defined. I'm trying with:

$query = new EntityFieldQuery();
$children = $query
    -&gt;entityCondition('entity_type', 'node')
    -&gt;entityCondition('bundle', 'page')
    -&gt;propertyCondition('status', 1)
    -&gt;fieldCondition('field_parent', 'target_id', null, 'IS NULL')
    -&gt;execute();


.. but that does not give anything as the field "field_parent" is missing and not set to NULL. If I use IS NOT NULL I get all nodes that has a parent.

How can I select all nodes that has no parent (or is missing the field "field_parent")?



Thanks to Clive I managed to solve this in the following way:

$query = new EntityFieldQuery();
$children = $query
    -&gt;entityCondition('entity_type', 'node')
    -&gt;entityCondition('bundle', 'page')
    -&gt;propertyCondition('status', 1)
    -&gt;addTag('filter_no_parent')
    -&gt;execute();

/**
* Implements hook_query_TAG_alter()
*/
function mymodule_query_filter_no_parent_alter(QueryAlterableInterface $query) {
    $query-&gt;leftJoin('field_data_field_parent', 'p', 'node.nid = p.entity_id');
    $query-&gt;isNull('p.field_parent_target_id');
}
You can't (natively) I'm afraid. An EntityFieldQuery doesn't have support for IS NULL or IS NOT NULL.

There are workarounds, however, essentially by adding a tag to the query and implementing a query alter hook, in which you add the required LEFT JOIN and IS NULL statement.

See EntityFieldQuery does not support isNull or isNotNull for a code example. I've used that code before with good results.