4

I am using the normal relatedTo property to find related articles, but I have an issue which is:

craft.entries.section('section').relatedTo("tags").id('not ' ~ entry.id).limit(3)

Lets say THIS article has 2 tags: "cat" and "animal", then I use the normal relatedTo property to get related articles. Lets say i want three articles

Sometimes I get these three:

  • article 1 (tags: "dog" and "animal")
  • article 2 (tags: "cat" and "food")
  • article 3 (tags: "mouse" and "animal")

But not this one:

  • article 4 (tags: "cat" and "animal")

How can i get the MOST related articles always? I want the one that has the most matching tags, not just random ones that has at least 1 tag.

Wobee
  • 328
  • 2
  • 9

2 Answers2

3

If you're only trying to find related articles based on elements (ie tags, categories, entries, etc), you can use my Similar plugin.

Here's a basic example, more info in the repo:

{% set similarEntriesByTags = craft.similar.find({ element: entry, context: entry.tags }) %}

<ul>
    {% for similarEntry in similarEntriesByTags %}
        <li>{{ similarEntry.title }} ({{ similarEntry.count }} tags in common)</li>
    {% endfor %}
</ul>
André Elvan
  • 7,288
  • 22
  • 34
1

One way to do this would be to use search instead of relatedTo(), which will return a searchScore attribute an all of the resulting elements that is a measure of how well the search match was.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143