0

The options in a views Advanced tab seem to only allow:

Multiple-value handling
Filter to items that share all terms
Filter to items that share any term

and I'd like to show items that share any term but sort by showing the items sharing the most terms at the top. Sort of like a "relevant" sort filter?

1 Answer 1

0

You can do this with Aggregation, but you have to be careful about some details.

The first thing to do is to create a block listing your desired content, with contextual filters that will restrict the results to only nodes that are tagged with the same terms.

A very popular method of doing that is through Has taxonomy term ID contextual filter set to Provide default value of Taxonomy term ID from URL (with other options appropriately set).

I haven't been able to achieve the Sorting through Aggregation that way. Perhaps someone will come along and shed some light on this, whether and how it can be done.

I have been able to do it with a different strategy, the one that is usually necessary when dealing with Entity references in general. It relies on Relationships to introduce sibling nodes, and ID as contextual filters. Here are the steps to create it:

List nodes tagged with the same Taxonomy terms

  1. Create a View of type Content, Filter it as usual to Content types, the taxonomy field not empty etc. Show fields Title and the field containing your terms, show Multiple values in the same row although that won't matter later, format as Table simply for readability, you can change it later.
  2. Add a Relationship (search for your field name) Taxonomy term referenced from field_yourtags.
  3. Add another Relationship Content using field_yourtags,
    it will be set by default to use the Relationship from the previous step
  4. Add a Contextual filter ID (from Content Category) and set it to use the second Relationship (the one from the previous step, it will actually be the only choice next to "Do not use a Relationship") with other options:
    When the filter value is NOT available:
    Provide default value
    Content ID from URL
    When the filter value IS available or a default is provided
    Specify validation criteria (optional)
    Content
    [your appropriate types]
  5. Add another Contextual filter ID but this time Do not use a Relationship:
    When the filter value is NOT available:
    Provide default value
    Content ID from URL
    More: Exclude

    This will exclude the node you're currently viewing from the list.

    If you want to check the output in Views Preview at this point enter the same node ID both times, for example to see what the block would output on node 14 enter 14/14.

    Don't worry about the multiplicates, we'll turn them to the best use with Aggregation ;) They are listed the number of times that the tags match between them and the current node!


    Aggregate results according to common terms count

  6. Turn Aggregation on (Use aggregation option in the third column set to Aggregate), don't worry about the red message in Preview. All fields and filters now have Aggregation settings. You only need to change the one on your taxonomy field:
  7. Click on Aggregation settings for the "Content: Yourtags" field and change it to Count DISTINCT. Notice how your tags are now replaced by numbers indicating how many common terms those nodes have with the current node!
  8. Now we just have to sort the View using this data: Add a Sort criteria:
    Content: Yourtags
    Do not use a Relationship
    Sort descending
    Aggregation settings: Count DISTINCT

Now you can Place your block in a Region and check the count on nodes.

Additional perk of this approach is that it won't list anything if the current node isn't tagged with any terms, while with the "Has taxonomy term ID" method you get all results.

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.