Group entitiy queries for a specific content type using Taxonomy Term Using Drupal 8 entity query I'm trying to figure how I can fetch values from a specific content type and group them that are associated via a specific taxonomy term. 

As seen in the screenshot below I have an entity field called Group Schedule that is currently binded to a Taxonomy Term. Is there way I could group those entities that are assigned to a specific taxonomy?

Group Schedule Name (field_group_sched)
First Schedule (field_created_date)
Second Schedule (field_created_date)

Group Schedule Name 2 (field_group_sched)
First Schedule (field_created_date)
Second Schedule (field_created_date)




So far I've managed to fetch the values from the Schedule Content type

// Entity Query for Schedule Node Type
$scheduleType = $query-&gt;get('node')
    -&gt;condition('status', 1, '=')
    -&gt;condition('type', 'schedule')
    -&gt;condition('field_associated_programs', $nid_degree, '=')
    -&gt;sort('created')
    -&gt;execute();

foreach ($scheduleType as $key =&gt; $program) {

    $programNode = _nodeLoad($program);

    $variables['schedule_content'][$key]['schedule_group'] = $programNode-&gt;get('field_group_sched')-&gt;value;
    $variables['schedule_content'][$key]['schedule_name'] = $programNode-&gt;get('field_schedule_name')-&gt;value;
    $variables['schedule_content'][$key]['schedule_date'] = $programNode-&gt;get('field_created_date')-&gt;value;


}




{% for program in schedule_content %}

&lt;div class="scheduled-dates"&gt;
    &lt;h3 class="label"&gt;{{ program['schedule_group'] }}&lt;/h3&gt;
    &lt;span data-label="{{ program['schedule_name'] }}"  class="date" &gt;{{ program['schedule_date'] }}&lt;/span&gt;
    &lt;/div&gt;


{% endfor %}
Ok well here we go if i understand you correct you want nodes linked by a entity reff field value:
in your example "field_group_sched" the see the below condition
but later you can just group stuff by anything you want 


   // this is the known term id. 
    $group_term_id = 1;
    // for Entity ref use field_name.target_id
    $query = \Drupal::entityQuery('node')
      ->condition('status', 1, '=')
      ->condition('type', 'schedule')
      ->condition('field_group_sched.target_id', $group_term_id, '=')
      ->sort('created');

    $entity_ids = $query->execute();
    if (count($entity_ids) != 0) {
      $entities = \Drupal::entityTypeManager()
        ->getStorage('node')
        ->loadMultiple($entity_ids);
      // dump($entities); // requires var dummper
      if (count($entities) != 0) {
        $group1 = [];
        $group2 = [];
        // then you can play with order or whatever you want here. 
        foreach ($entities as $node) {
          // These are the loaded nodes;
          $title = $node->getTitle();
          $field_name  = $node->field_schedule_name->value;
          // if the schedule_name is "beer" put in to group 1
          if ($field_name == 'beer') {
            $group1[] = $node;
          }
          else {
            $group2[]= $node;
          }
        }
        // SEND your Nodes to the template here. 
      }
    }