Limit views exposed filter options to terms with associated nodes of content type X I have a module which limits the options in a views exposed filter to terms which have associated nodes. The code is based on this answer: How can I limit the exposed filters to only display values for which content exists?
Code:
if (isset($form['area-of-law']['#options'])) {

  foreach ($form['area-of-law']['#options'] as $key =&gt; $value) {

    if (is_numeric($key)) {
      $query = \Drupal::database()-&gt;select('taxonomy_index', 'ti');
      $query-&gt;fields('ti', ['nid']);
      $query-&gt;condition('ti.tid', $key);
      $nodes = $query-&gt;execute()-&gt;fetchAssoc();

      if (!$nodes) {
        unset($form['area-of-law']['#options'][$key]);
      }
    }
  }
}

This works, but all node types are returned. I would like to limit the filter further to only show those of content type X.
$query = \Drupal::database()-&gt;select('taxonomy_index', 'ti');
  $query-&gt;condition('ti.tid', $key);
  $query-&gt;join('node', 'n', 'n.nid = ti.nid'); // join node table
  $query-&gt;condition('n.type', 'x'); // filter by type
  $query-&gt;fields('n', ['nid']); // add filtered nids
  $nodes = $query-&gt;execute()-&gt;fetchAssoc();