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/… Commented Apr 4, 2017 at 17:00
  • @tenken whoops, that was a typo, thanks, I went ahead and made the update 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(); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.