<p>Not sure about doing this in a single query. Referencing this doc page section, <a href="https://docs.craftcms.com/v3/relations.html#templating" rel="nofollow noreferrer">https://docs.craftcms.com/v3/relations.html#templating</a>, you can make use of <code>relatedTo()</code> by passing in IDs.</p>

<p>Assuming you know the ID of the category already and that ID is <code>1</code>:</p>

<pre><code>{% set showIds = craft.entries.section('shows').relatedTo(1).ids() %}

{% set entries = craft.entries.section('events').relatedTo(showIds).all() %}
</code></pre>

<p>The <code>relatedTo()</code> method doesn't care if the IDs are from Entries or Categories, or even Users and Tags. They're all Elements stored in the <code>elements</code> table so they have unique IDs.</p>

CC BY-SA 4.0
2018-08-10T09:27:03.837Z
Yes, I went for this solution also, is the less hacky I've found so far. I still think there should be a way to join, but for now I managed doing something like that.
Jorge Anzola
CC BY-SA 4.0
2018-08-09T20:40:28.463Z
2018-08-09T20:40:28.463Z
Sam Hernandez
<p>I've been trying this for a while now, so might have read this before. Sorry about that, I'm struggling really hard to get this right. At first I tried (wrongly) with the eager load features.</p>

<p>I have two channels: <code>events</code> and <code>shows</code>. In the <code>events</code> you can select a <code>show</code> with an entry field. The shows items have a category attached. I need to filter the <code>events</code>, based on the category of the <code>show</code></p>

<p>Like <code>events with shows where show has category 1</code>. They told me on the slack channel to join the tables I needed, but cannot manage to do it.</p>

<entries><categories><mysql><query><elementquery>
Joining sections (Channel) to query based on a related item attribute
# Joining sections (Channel) to query based on a related item attribute

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

I've been trying this for a while now, so might have read this before. Sorry about that, I'm struggling really hard to get this right. At first I tried (wrongly) with the eager load features.

I have two channels: `events` and `shows`. In the `events` you can select a `show` with an entry field. The shows items have a category attached. I need to filter the `events`, based on the category of the `show`

Like `events with shows where show has category 1`. They told me on the slack channel to join the tables I needed, but cannot manage to do it.

**Tags:** entries, categories, mysql, query, elementquery

---

## 1 answers

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

Not sure about doing this in a single query. Referencing this doc page section, https://docs.craftcms.com/v3/relations.html#templating, you can make use of relatedTo() by passing in IDs.
Assuming you know the ID of the category already and that ID is 1:
{% set showIds = craft.entries.section('shows').relatedTo(1).ids() %}

{% set entries = craft.entries.section('events').relatedTo(showIds).all() %}

The relatedTo() method doesn't care if the IDs are from Entries or Categories, or even Users and Tags. They're all Elements stored in the elements table so they have unique IDs.

> **Comments:**
> **username_0**: Yes, I went for this solution also, is the less hacky I've found so far. I still think there should be a way to join, but for now I managed doing something like that.  

---

