Skip to main content

Drupal 8 entityQuery example for getting all the reference entities from a reference field

It was kind of hard to get an example entity field query for drupal 8 that returns all the related content of an entity reference field. So here it is.

//Get how many you want
$counter = 1000;
$query = \Drupal::entityQuery('node');
$query->condition('status', 1);
$query->condition('type', 'your_bundle');
$query->sort('nid', 'ASC');
$query->range(0, $counter);
$nids = $query->execute();