1

I have a content type that has a node entity reference field. My base query looks like

$event_nids = \Drupal::entityQuery('node')
    ->condition('type', 'my_content_type')
    ->execute();

I'd like though to be able to sort by the title field of the referenced field type. I attempted

$event_nids = \Drupal::entityQuery('node')
    ->condition('type', 'my_content_type')
    ->sort('field_referenced_node.title', ASC)
    ->execute();

But I receive the error

Drupal\Core\Entity\Query\QueryException: Invalid specifier 'title' in Drupal\Core\Entity\Query\Sql\Tables->addField()

Is it possible to sort a node query by a field inside a referenced node?

2
  • 1
    Sure but you probably want to sort and not use a condition for this api.drupal.org/api/drupal/…
    – tenken
    Commented Apr 4, 2017 at 17:00
  • @tenken whoops, that was a typo, thanks, I went ahead and made the update
    – Amy
    Commented Apr 4, 2017 at 18:01

1 Answer 1

3

I believe you want something like below. Note that this isn't really a database query at this level but an Entity Query:

$event_nids = \Drupal::entityQuery('node')
  ->condition('type', 'my_content_type')
  ->sort('field_referenced_node.entity.title', ASC)
  ->execute();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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