2

I have succesfully shown some related entries that are in the same category, using the following:

{% set relatedEntries = craft.entries({
    section: 'blog',
    relatedTo: entry.blogCategory.first(),
    id: 'not ' ~ entry.id,
    limit: 3
}) %}           

(Entries generally only have 1 category on this site).

What I would like to do next, is look for entries that have some of the same tags as the current entry, making this list of entries even more relevant.

Essentially I want the related entries to be as relevant as possible, this could even go as far as ignoring the Category if there are more tags in common, is that possible by somehow grading the results and displaying the most 'in-common' entries?

2

If you don't mind using a plugin, Similar would work very well for your use case:

{% set relatedEntries = craft.similar.find({
    element: entry,
    context: entry.tagsField.ids()|merge(entry.blogCategory.ids()),
    criteria: {
        section: 'blog',
        limit: 3
    }
}) %}

Of course, be sure to replace tagsField in the above example with the actual handle for your entry type's Tags field.

By default, the entries returned by Similar will be ordered after the number of relations in common with the current entry (or whatever you pass into the element parameter). In other words, the entry with the most tags and/or categories in common with the current entry will be the first. The entries returned by Similar will also have an entry.count attribute that will indicate the number of relations in common with the current entry.

If you want to do this without a plugin, it's possible to add multiple criteria to the relatedTo parameter in order to pull entries that has either one or more categories or tags in common with the current entry:

{% set relatedEntries = craft.entries({
    section: 'blog',
    relatedTo: ['or', {
        targetElement: entry.blogCategory.ids(),
        field: 'blogCategory'
    }, {
        targetElement: entry.tagsField.ids(),
        field: 'tagsField'
    }],
    id: 'not ' ~ entry.id,
    limit: 3
}) %}

I'm not aware of a way to query the EntriesService for the 3 entries with the most relations in common with entry, though (the lack of such a feature is why the Similar plugin exists). I guess this could be achieved in plain Twig by possibly setting the limit parameter to null (to return all entries – yikes! performance), looping over the returned entries and comparing the number of relations in entry.tagsField and entry.blogCategory to each related entry and add the entries to an array ordered by the amount of relations somehow, but I suspect it will get pretty dirty and quickly go beyond what Twig is designed to comfortably do, logic-wise.

  • Thanks Mats, I'll give the plugin a try, seems to be perfect to what I need. Thanks for your detailed response! – Adam Menczykowski Aug 9 '16 at 21:31
  • 1
    Using your Similar plugin works a treat, now categories and tags are taken into account, and also the generated SQL queries has gone down from 12 to 11. – Adam Menczykowski Aug 22 '16 at 12:31
  • @AdamMenczykowski Awesome! I'm not the author of Similar though; André Elvan is :) – Mats Mikkel Rummelhoff Aug 22 '16 at 12:33

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.