<p>The <code>relatedTo</code> param expects an object, an ID or an array of object or ID (<a href="https://docs.craftcms.com/v3/relations.html#the-relatedto-parameter" rel="nofollow noreferrer">docs</a>), in your case <code>secondSegment</code> is a string and this is the issue. Try:</p>

<pre><code>{% set tag = craft.tags.slug( craft.app.request.getSegment(2) ).one() %}
{% set entries = craft.entries.relatedTo(tag).all() %}
</code></pre>

CC BY-SA 4.0
2019-11-08T07:18:08.827Z
Thank you! That works.
Adam George
CC BY-SA 4.0
2019-11-07T16:48:11.183Z
2019-11-07T16:48:11.183Z
Oli
<p>I've read through the docs but can't figure out what I'm doing wrong here:</p>

<p><strong>I'd like to display a list of entries by tag, where the tag is in segment 2 of the url.</strong></p>

<p>Here's what I've tried and is not working (they output nothing):</p>

<pre><code>{% set secondSegment = craft.app.request.getSegment(2) %}

{% set diaryEntries = craft.entries.section(['gardenDiary','gardenInfo']).relatedTo({
        sourceSite: craft.app.sites.currentSite.id, 
    element: secondSegment,
    field: 'gardenTags'
}).limit(null).all() %}
</code></pre>

<p>Also tried:</p>

<pre><code>{% set diaryEntries = craft.entries.section('gardenDiary').relatedTo(secondSegment).all() %}
</code></pre>

<p>...</p>

<pre><code>{% for entry in diaryEntries %}
{{ entry.title }}
...etc...
{% endfor %}
</code></pre>

<ul>
<li>Entries are known to be related to tags</li>
<li>I've tried adding a known related tag directly - still nothing.</li>
<li>If I don't specify a tag, it lists all the entries.</li>
<li>Example URL: mydomain.com/tags/fruit</li>
<li>Template is loaded via a Route: [Global] tags/* -->  tags/index.html</li>
</ul>

<p>What am I doing wrong? Thanks in advance.</p>

<tags>
Display entries by tag
# Display entries by tag

(Asked by: username_0 on 2019-11-07)

I've read through the docs but can't figure out what I'm doing wrong here:
I'd like to display a list of entries by tag, where the tag is in segment 2 of the url.
Here's what I've tried and is not working (they output nothing):
{% set secondSegment = craft.app.request.getSegment(2) %}

{% set diaryEntries = craft.entries.section(['gardenDiary','gardenInfo']).relatedTo({
        sourceSite: craft.app.sites.currentSite.id, 
    element: secondSegment,
    field: 'gardenTags'
}).limit(null).all() %}

Also tried:
{% set diaryEntries = craft.entries.section('gardenDiary').relatedTo(secondSegment).all() %}

...
{% for entry in diaryEntries %}
{{ entry.title }}
...etc...
{% endfor %}


Entries are known to be related to tags
I've tried adding a known related tag directly - still nothing.
If I don't specify a tag, it lists all the entries.
Example URL: mydomain.com/tags/fruit
Template is loaded via a Route: [Global] tags/* -->  tags/index.html

What am I doing wrong? Thanks in advance.

**Tags:** tags

---

## 1 answers

### Answer by username_1 on 2019-11-07. Score: 1 | Accepted answer

The relatedTo param expects an object, an ID or an array of object or ID (docs), in your case secondSegment is a string and this is the issue. Try:
{% set tag = craft.tags.slug( craft.app.request.getSegment(2) ).one() %}
{% set entries = craft.entries.relatedTo(tag).all() %}

> **Comments:**
> **username_0**: Thank you! That works.  

---

