<p>You need to fetch the actual tag before passing it into <code>relatedTo()</code>. The data you have to work with in your example (&quot;adventure&quot;) is a tag <em>slug</em>, so query for that using <code>.slug()</code>:</p>
<pre><code>{% set tag = craft.tags.group('genre').slug('adventure').one() %}
{% if tag %}
    {% set entries = craft.entries.relatedTo(tag).all() %}
{% endif %}
{% dd entries %}
</code></pre>
<p>(...and don't forget to also execute your entries query with <code>.all()</code>).</p>
<p>I'm afraid I don't quite understand your other question about creating an &quot;intermediary field&quot;...? All you need to do is 1) create a tag group and 2) create a field that allows entries to select tags, optionally filtered by that group if you have multiple groups.</p>

CC BY-SA 4.0
2022-09-12T10:44:20.730Z
2022-09-12T15:02:05.690Z
2022-09-12T15:02:05.690Z
James Smith
<p>Let's say I have a tag group with handle <strong>genre</strong>.</p>
<p>I can make an entry contain information about this tag by creating a field of type tag that uses this group.</p>
<p>So I would have a field with handle <strong>genreTag</strong> added to the entry with handle <strong>movies</strong>.</p>
<p><em>(Note: I don't see the point of having to create an intermediary field, instead of just adding the tag selector to the entry. Why would I have multiple fields for the same tag group?)</em></p>
<p>Now I need to query entries that have a genre of adventure, for example.</p>
<p>I read <a href="https://craftcms.stackexchange.com/questions/32950/display-entries-by-tag">this question</a>, <a href="https://craftcms.stackexchange.com/questions/14083/query-entries-by-tag">this question</a>, <a href="https://craftcms.com/docs/4.x/tags.html" rel="nofollow noreferrer">the docs</a>, and still don't get it:</p>
<p>As far as I understand, it would be done as follows:</p>
<pre><code>{% set tag = craft.tags.genre('adventure').one() %}
{% if tag %}
    {% set entries = craft.entries.relatedTo(tag) %}
{% endif %}
{% dd entries %}
</code></pre>
<p><a href="https://i.sstatic.net/Qp9Cm.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Qp9Cm.png" alt="enter image description here" /></a></p>
<p>And doing it by the field handle instead of the tag handle:</p>
<pre><code>{% set tag = craft.tags.genreTag('adventure').one() %}
{% if tag %}
    {% set entries = craft.entries.relatedTo(tag) %}
{% endif %}
{% dd entries %}
</code></pre>
<p><a href="https://i.sstatic.net/6zqyG.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6zqyG.png" alt="enter image description here" /></a></p>
<p>How should I approach this?</p>
<p>Thanks!</p>

<query><tag>
Get entries where tag = value
# Get entries where tag = value

(Asked by: username_0 on 2022-09-12)

Let's say I have a tag group with handle genre.
I can make an entry contain information about this tag by creating a field of type tag that uses this group.
So I would have a field with handle genreTag added to the entry with handle movies.
(Note: I don't see the point of having to create an intermediary field, instead of just adding the tag selector to the entry. Why would I have multiple fields for the same tag group?)
Now I need to query entries that have a genre of adventure, for example.
I read this question, this question, the docs, and still don't get it:
As far as I understand, it would be done as follows:
{% set tag = craft.tags.genre('adventure').one() %}
{% if tag %}
    {% set entries = craft.entries.relatedTo(tag) %}
{% endif %}
{% dd entries %}


And doing it by the field handle instead of the tag handle:
{% set tag = craft.tags.genreTag('adventure').one() %}
{% if tag %}
    {% set entries = craft.entries.relatedTo(tag) %}
{% endif %}
{% dd entries %}


How should I approach this?
Thanks!

**Tags:** query, tag


---

## 1 answers

### Answer by username_1 on 2022-09-12. Score: 1 | Accepted answer

You need to fetch the actual tag before passing it into relatedTo(). The data you have to work with in your example ("adventure") is a tag slug, so query for that using .slug():
{% set tag = craft.tags.group('genre').slug('adventure').one() %}
{% if tag %}
    {% set entries = craft.entries.relatedTo(tag).all() %}
{% endif %}
{% dd entries %}

(...and don't forget to also execute your entries query with .all()).
I'm afraid I don't quite understand your other question about creating an "intermediary field"...? All you need to do is 1) create a tag group and 2) create a field that allows entries to select tags, optionally filtered by that group if you have multiple groups.


---

