1

I have nodes with tags on them. I also have a view of nodes with a contextual filter that can take multiple tags. I have it set to get nodes that match any, rather than all.

So imagine I have three nodes, Lorem, Ipsum, and Dolor. They have the following tags:

Lorem: foo, bar, baz Ipsum: foo, bar Dolor: foo

When the contextual filter "foo" is passed, of course all three come up. When "foo" and "bar" are both passed, all three also come up. Currently, they come up in no particular order, so passing "foo" and "bar" might give me this (depending on my other filters):

  1. Dolor
  2. Lorem
  3. Ipsum

I want to sort by number of matched items, so passing "foo" and "bar" will bring them up in the order Lorem, Ipsum, Dolor or possibly Ipsum, Lorem, Dolor, but never with Dolor first. And if I pass all three terms I'd expect to see the only the order Lorem, Ipsum, Dolor.

This seems like a common thing to need, and yet searching has not yielded a ready-made solution. How would one go about implementing this with Views 3 in Drupal 7?

1 Answer 1

3

Following is one way to achieve the specified ordering

  1. Add a relationship to taxonomy terms.

enter image description here

  1. Turn on aggregation

  2. Set the sort criteria to COUNT DISTINCT of taxonomy term reference field.

3
  • Won't this just sort by the number of terms on the node total? Like if I have "foo" and "bar" in common but also "zzz" and "lmno" appear, the position would be "4" rather than "2"?
    – beth
    Commented Aug 17, 2016 at 20:30
  • Yes @beth, I have updated the solution now. Hope this helps
    – ajp
    Commented Aug 18, 2016 at 7:25
  • I tried this...but it doesn't work for me. I have the problem that is mentioned in the first comment. It counts all my terms. What do you do with the relationship apart from adding it?
    – Joost
    Commented Nov 7, 2022 at 15:18

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.