<p>If you don't mind using a plugin, <a href="https://github.com/aelvan/Similar-Craft" rel="nofollow">Similar</a> would work very well for your use case:</p>

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

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

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

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

<pre><code>{% 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
}) %}
</code></pre>

<p>I'm not aware of a way to query the EntriesService for the 3 entries with the most relations in common with <code>entry</code>, 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 <code>limit</code> parameter to <code>null</code> (to return all entries – yikes! performance), looping over the returned entries and comparing the number of relations in <code>entry.tagsField</code> and <code>entry.blogCategory</code> 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.</p>

CC BY-SA 3.0
2016-08-09T21:31:14.393Z
Thanks Mats, I'll give the plugin a try, seems to be perfect to what I need. Thanks for your detailed response!
Adam Menczykowski
CC BY-SA 3.0
2016-08-22T12:31:53.563Z
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
CC BY-SA 3.0
2016-08-22T12:33:17.117Z
@AdamMenczykowski Awesome! I'm not the author of Similar though; [André Elvan](http://craftcms.stackexchange.com/users/141/andr%C3%A9-elvan) is :)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-08-09T19:37:53.680Z
2016-08-09T20:00:18.603Z
2016-08-09T20:00:18.603Z
Mats Mikkel Rummelhoff
<p>I have succesfully shown some related entries that are in the same category, using the following:</p>

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

<p>(Entries generally only have 1 category on this site).</p>

<p>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.</p>

<p>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?</p>

<categories><relations><tags>
Show related entries that have the same Category, and some of the same tags?
# Show related entries that have the same Category, and some of the same tags?

(Asked by: username_0 on 2016-08-09)

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?

**Tags:** categories, relations, tags

---

## 1 answers

### Answer by username_1 on 2016-08-09. Score: 2 | Accepted answer

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.

> **Comments:**
> **username_0**: Thanks Mats, I'll give the plugin a try, seems to be perfect to what I need. Thanks for your detailed response!  
> **username_0**: 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.  
> **username_1**: @AdamMenczykowski Awesome! I'm not the author of Similar though; [André Elvan](http://craftcms.stackexchange.com/users/141/andr%C3%A9-elvan) is :)  

---

