Most related entries #13161
-
On the page of a news article we would like to show other related news articles based on 3 different Category groups. We would like to sort these related articles on "most related" so entries with the most categories in common. My code however orders the entries based on postDate, most recent first. My code:
I have tried to add Can someone give me a hand? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
There’s no built-in way to accomplish this yet, but it’s been done before with some custom code / Preparse fields. See https://craftcms.stackexchange.com/q/1091/9 for a couple pointers |
Beta Was this translation helpful? Give feedback.
-
The Similar plugin could be helpful for your use case: {% set categories = entry.catergoryA.ids()|merge(entry.catergoryB.ids())|merge(entry.catergoryC.ids()) %}
{% set news = craft.similar.find({ element: entry, context: categories, criteria: craft.entries.section('news').limit(4) }) %} With the above, the |
Beta Was this translation helpful? Give feedback.
The Similar plugin could be helpful for your use case:
With the above, the
news
variable would be an array of (maximum 4) entries related to the category IDs from those three category fields; automatically sorted by the number of relations (the entries will also have an additionalcount
attribute, referencing the total number of relations for each entry).