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.

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>
| improve this answer | |
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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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