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')
->condition('status', NODE_PUBLISHED)
->condition('type', 'issue');
$nids = $query->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->field_publication->entity;
$pub_permission = $pub->field_permission_category->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')
->condition('status', NODE_PUBLISHED)
->condition('type', 'issue')
->sort('field_publication->entity->field_permission_category->value', DESC);
Can you sort a query by a field inside a referenced node?
->sort('field_publication.entity.field_permission_category')