I created a views that filter nodes according taxonomy terms passed through contextual filters.

  • Node 1: term1, term2, term3
  • Node 2: term1, term6
  • Node 3: term1, term7

Everything is working good, expect i would like to display the number of term tid found in the node.

When i use the parameters term1+term7 (in contextual filters), i would like a result like this :

  • Node title 1: match 1
  • Node title 2: match 1
  • Node title 3: match 2

and then sort results by the matching terms.

How can i make it work with Views 3 in Drupal 8?

Many thanks.

My view

1 Answer 1


There are two parts to this question: getting the Contextual filtering to reduce the results to only the term IDs you specify in the URL, and using Aggregation for counting and sorting the results according to that count.

Contextual filter for Taxonomy term ID

  1. Create your Views list of nodes with the Taxonomy reference field showing the tags, and configure that field to show all values in the same row
  2. Add the Contextual filter for your Taxonomy term field or for the Field Has taxonomy term ID and configure it:
    When the filter value is NOT available
    Provide default value
    Taxonomy term ID from URL
    Load default filter from node page, that's good for related taxonomy blocks
    Filter to items that share any term
    When the filter value IS available or a default is provided
    Specify validation criteria
    Taxonomy term ID
    [your vocabulary]
    One or more IDs separated by , or +
    Allow multiple values

Test this by entering the term ID (number) into the Views Preview field. You should see the results reduce to show nodes tagged with that term. Also test for more than one term, enter, for example, 3+5 to show nodes that are tagged with either terms 3 or 5, or 3,5 to show nodes tagged with both terms 3 and 5.

If you want to use term names instead of term ID numbers, then instead of using the taxonomy term reference field as the Contextual filter, you'll have to first add a Relationship for that field, then you will have the Taxonomy Name Contextual filter available. You configure it similarly as above, be careful about case options. You test it by entering the term names into Preview: firstTerm,secondTerm.

Count with Aggregation

  1. Turn Aggregation on in the third column of Views settings
  2. Click on Aggregation settings for the Taxonomy field (from step 1. above) and change the Aggregation type to Count DISTINCT.

    This field will now show the count how many terms are shared.

  3. Add the Sort criteria for your Taxonomy terms field, and set its Aggregation to Count DISTINCT and you'll probably want to sort descending. You should have no other sorting added except this. You will see the nodes sorted by the number of tags shared with the contextual filter.

Test this the same way as above, entering term IDs, or term names if that's what you're using.

In the end you'll have to test in a real page to check that the Contextual filter has been configured properly. Build and test in steps, don't first build everything and only test because if there's something wrong it will be more difficult to figure out what and you'll have to start from scratch step by step anyway.

  • Hi Prkos Thank you for your aswser .. the view display good results .. it show all tags referenced to the node .. after i enable distinct and click Aggregation to Count DISTINCT on field ttags. it count the number of of tags referenced to node .. and not the count of tags matching contextual filters.. any suggestions ?
    – Eacute
    Commented Sep 24, 2019 at 7:41
  • I haven't used Distinct from Query settings, but I'm guessing it won't matter since it should be about regular views results which are nodes, not taxonomy terms. I'm getting the correct count, although I had to use Count instead of Count DISTINCT to count correctly when I had the same term used more than once on a node. I'm having trouble with the + and , operators on an example I'm trying out. Have added more fields that need to have the Aggregation adjusted? Because Aggregation settings on different fields can affect how results are displayed/counted.
    – prkos
    Commented Sep 24, 2019 at 14:58
  • I used "count distinct" only on the field "Content: tag" .. I am not having trouble with "+" operator all parameters a taken into account with "+" .. the only problem is that all tags associacted to node a are displayed and when i add the parameters of aggregation "count distinct " the count displayed is the cpount of tags associated .. not the one matched from contextual filters .
    – Eacute
    Commented Sep 24, 2019 at 15:10
  • Here's the issue related to the contextual multivalue filtering: Contextual filter issues while passing multiple taxonomy selections from node, to pages, to views.
    – prkos
    Commented Sep 24, 2019 at 17:17
  • 1
    Hi Pkos thank you for help the problem was the contextual filter "Has taxonomy ID" Once i changed to "Content: Tag" i got the correct count of matched terms.
    – Eacute
    Commented Sep 25, 2019 at 8:59

Your Answer

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

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