Query and sort by field on entity referenced object I have a custom content type that has an entity reference field, that refers to an existing node from a different content type.

I'm performing the query like so

$query = \Drupal::entityQuery('node')
-&gt;condition('status', NODE_PUBLISHED)
-&gt;condition('type', 'issue');

$nids = $query-&gt;execute();


Each of these issue nodes have a field_publication field that refers to a publication node. Each of these publication nodes have a field_permission_category that I'm grabbing like so.

foreach ($nids as $nid) {
   $node = NODE::load($nid);
   $pub = $node-&gt;field_publication-&gt;entity;
   $pub_permission = $pub-&gt;field_permission_category-&gt;value;
}


But I want to be able to order the results of the initial query by the value of field_permission_category from the referenced field, but I'm not sure how to go about that, and I'm not even sure that's possible. Basically I want to be able to do

$query = \Drupal::entityQuery('node')
-&gt;condition('status', NODE_PUBLISHED)
-&gt;condition('type', 'issue')
-&gt;sort('field_publication-&gt;entity-&gt;field_permission_category-&gt;value', DESC);


Can you sort a query by a field inside a referenced node?
The answer by Clive is correct.
I  used this query for a project :

$query-&gt;sort('field_projets.entity.field_date_mise_en_prod', 'DESC');


'field_projets' is the field name of the entity reference field on my node.

And 'field_date_mise_en_prod' is the name of a date field belonging to the referenced entity .

You can also use this syntax on conditions.
Exemple :

$query-&gt;condition('field_tags.entity.name', 'cats');


You asking for a tag with the name 'cats'.