sorry for my English.

In views, Drupal 8/9, is it possible to have the count of nodes for a taxonomy-term with depth? I would like to have it for the filters in views.

I can't find any modules to get this information. Maybe I don't know how to look or a complex module gives this possibility but I couldn't find it.

Aggregation doesn't really help me. Performance is not a problem.

  • Search for answers with Views Contextual filter Summary examples, it may be what you're looking for.
    – prkos
    Feb 21, 2021 at 23:21
  • Thanks but we can't tweak anything on the result.
    – tronic
    Feb 22, 2021 at 18:12
  • 1
    Sounds like core view facets is what you need.
    – No Sssweat
    Feb 24, 2021 at 5:14

2 Answers 2


There are several ways to do it, based on how you are going to manage your content.
But for your problem I guess the solution can be this:

1. In Fileds: add Taxonomy term: Name
2. In Relationships, add a Taxonomy term: Content with term
3. Add a contextual filter for Taxonomy term: Name
    3a. Choose 'Display a summary'
    3b. Choose 'Display Record count with link'.

Here is several resources that can help:
Views and node count for taxonomy terms
How to display number of nodes with current term?
Showing how many nodes are associated with a term
Display node count per node reference field

  • I found these links, but nothing interesting in relation to my need for "depth" and "filters"
    – tronic
    Feb 28, 2021 at 18:19

We have to write a module, something like this


function childs_count_views_data()

  $data['taxonomy_term_field_data']['childs_count'] = array(
    'title' => t('Childs count'),
    'help' => t('Get the number of childs in taxo.'),
    'field' => array(
      'id' => 'childs_count'
    'filter' => [
      'id' => 'childs_count_filter',

  return $data;



//to do add limit and type..
function childs_count_query($depth = 5)
  if ($depth < 1) {
    $depth = 1;
  $sql = "(
  SELECT COUNT(nid) FROM node_field_data WHERE node_field_data.nid IN (
    SELECT tn.nid AS nid FROM taxonomy_index tn
            LEFT JOIN taxonomy_term__parent th ON th.entity_id = tn.tid
            LEFT JOIN taxonomy_term__parent th1 ON th.parent_target_id = th1.entity_id";
  foreach (range(1, $depth - 1) as $level) {
    $sql .= "\n\tLEFT JOIN taxonomy_term__parent th" . ($level + 1) . " ON th" . $level . ".parent_target_id = th" . ($level + 1) . ".entity_id\n";

  $sql .= "WHERE (tn.tid = taxonomy_term_field_data.tid)";
  foreach (range(1, $depth) as $level) {
    $sql .= "\nOR (th$level.entity_id = taxonomy_term_field_data.tid)";
  $sql .= ")\n)\n";

  return $sql;


namespace Drupal\childs_count\Plugin\views\field;

use Drupal\views\Plugin\views\field\NumericField;

 * Field handler for search snippet.
 * @ingroup views_field_handlers
 * @ViewsField("childs_count")
class ChildsCount extends NumericField

  public function query()

    $this->query->addField(NULL, childs_count_query(), $this->field_alias);




namespace Drupal\childs_count\Plugin\views\filter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\NumericFilter;

 * Defines a filter for filtering on numeric values.
 * @ingroup views_filter_handlers
 * @ViewsFilter("childs_count_filter")
class ChildsCountFilter extends NumericFilter

  public function query()
    $where = $this->operator . ' ' . $this->value['value'];
    $this->query->addWhereExpression($this->options['group'], childs_count_query() . " $where");

   * {@inheritdoc}
  public function operators()
    $operators = parent::operators();
    unset($operators['not between']);
    return $operators;

  protected function valueForm(&$form, FormStateInterface $form_state)
    parent::valueForm($form, $form_state);

I will do it. it's not good for server performance, but it suits me. Thanks for your help.

Your Answer

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

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