<p>After chatting on Discord a bit more, here's a solution in case it helps somebody:</p>

<pre class="lang-twig prettyprint-override"><code>{# Base query #}
{% set baseQuery = craft
    .entries
    .section('stories')
    .orderBy('RAND()')
    .with(['storyKeywords']) %}

{# Fetch one story at random #}
{% set baseStory = baseQuery.one() %}

Base random story: {{ baseStory.title }}&lt;br&gt;

{# Fetch a second story related to `baseStory` at random #}
{% set relatedStory = clone(baseQuery)
    .relatedTo({
        targetElement: baseStory.storyKeywords,
        field: 'storyKeywords'
    })
    .id('not ' ~ baseStory.id)
    .one() %}

Related Story: {{ relatedStory.title }}&lt;br&gt;

{% set baseKeywords = baseStory.storyKeywords %}
{% set relatedKeywords = relatedStory.storyKeywords %}

&lt;hr&gt;
All tags in common: {{ baseKeywords | filter((tag) =&gt; tag.title in relatedKeywords) | join(', ') }} &lt;br&gt;
Number of tags in common: {{ baseKeywords | filter((tag) =&gt; tag.title in relatedKeywords) | length }}
</code></pre>

CC BY-SA 4.0
2020-05-05T13:04:43.253Z
This is working like a dream! Thank you.
richardpixel
CC BY-SA 4.0
2020-05-05T13:03:08.190Z
2020-05-05T13:08:16.647Z
2020-05-05T13:08:16.647Z
Oli
<p>You could do the following:</p>

<pre><code>{# Fetch tags from your current entry's tag field #}
{% set tags = entry.tagFieldHandle.ids() %}

{# Query entries related to your tags #}
{% set entries = craft.entries.relatedTo(tags).all() %}
</code></pre>

CC BY-SA 4.0
2020-05-04T14:43:56.020Z
Thanks but still a little confused. What IDs would I be putting here? I don't want to specify any specific tags or entries, I just need to get back 2 random entries which share at least 2 tags (ideally).

Does that make sense?
richardpixel
CC BY-SA 4.0
2020-05-04T13:32:34.260Z
2020-05-04T13:32:34.260Z
Oli
<p>I need to be able to display entries (within one section) that share keywords. I don't care what the keywords are, just want to display the entries based on 2 or more shared keywords.</p>

<p>Anyone know how to go about this?</p>

CC BY-SA 4.0
2020-05-01T13:26:55.533Z
Are you adding tags (as per your title) to the entry or you want to compare two block of text for words in common?
Oli
CC BY-SA 4.0
2020-05-01T17:18:39.413Z
Tags will be added to the entries and it's those that we need to use to link the entries together.
richardpixel
CC BY-SA 4.0
2020-05-04T09:44:41.820Z
Any ideas @Oli?
richardpixel
<relations><tags>
Display entries which share tags (2 or more)
# Display entries which share tags (2 or more)

(Asked by: username_0 on 2020-04-28)

I need to be able to display entries (within one section) that share keywords. I don't care what the keywords are, just want to display the entries based on 2 or more shared keywords.

Anyone know how to go about this?

**Tags:** relations, tags

> **Comments:**
> **username_1**: Are you adding tags (as per your title) to the entry or you want to compare two block of text for words in common?  
> **username_0**: Tags will be added to the entries and it's those that we need to use to link the entries together.  
> **username_0**: Any ideas @username_1?  

---

## 2 answers

### Answer by username_1 on 2020-05-05. Score: 2 | Accepted answer

After chatting on Discord a bit more, here's a solution in case it helps somebody:
{# Base query #}
{% set baseQuery = craft
    .entries
    .section('stories')
    .orderBy('RAND()')
    .with(['storyKeywords']) %}

{# Fetch one story at random #}
{% set baseStory = baseQuery.one() %}

Base random story: {{ baseStory.title }}

{# Fetch a second story related to `baseStory` at random #}
{% set relatedStory = clone(baseQuery)
    .relatedTo({
        targetElement: baseStory.storyKeywords,
        field: 'storyKeywords'
    })
    .id('not ' ~ baseStory.id)
    .one() %}

Related Story: {{ relatedStory.title }}

{% set baseKeywords = baseStory.storyKeywords %}
{% set relatedKeywords = relatedStory.storyKeywords %}


All tags in common: {{ baseKeywords | filter((tag) => tag.title in relatedKeywords) | join(', ') }} 
Number of tags in common: {{ baseKeywords | filter((tag) => tag.title in relatedKeywords) | length }}

> **Comments:**
> **username_0**: This is working like a dream! Thank you.  

---

### Answer by username_1 on 2020-05-04. Score: 1

You could do the following:
{# Fetch tags from your current entry's tag field #}
{% set tags = entry.tagFieldHandle.ids() %}

{# Query entries related to your tags #}
{% set entries = craft.entries.relatedTo(tags).all() %}

> **Comments:**
> **username_0**: Thanks but still a little confused. What IDs would I be putting here? I don't want to specify any specific tags or entries, I just need to get back 2 random entries which share at least 2 tags (ideally).  Does that make sense?  

---

