<p>If you are trying to get all <code>people</code> entries related to the <code>event</code> entry itself (what you call event &quot;e&quot;) then I would suggest <a href="https://craftcms.com/docs/3.x/relations.html#going-through-matrix" rel="nofollow noreferrer">going through matrix</a> as follows.</p>
<pre><code>{% set people = craft.entries.section('people').relatedTo([
    'or',
    { targetElement: event, field: 'matrixFieldHandle.relatedEvents' },
    { targetElement: event, field: 'matrixFieldHandle.relatedArticles' }
]) %}
</code></pre>
<p>If you need the target element to be all children of the <code>event</code> then this might do the trick.</p>
<pre><code>{% set eventChildren = event.children %}

{% set people = craft.entries.section('people').relatedTo([
    'or',
    { targetElement: eventChildren, field: 'matrixFieldHandle.relatedEvents' },
    { targetElement: eventChildren, field: 'matrixFieldHandle.relatedArticles' }
]) %}
</code></pre>
<p>You can also combine the <code>event</code> with its children, if necessary, as follows.</p>
<pre><code>{% set eventWithChildren = event.children|push(event) %}
</code></pre>

CC BY-SA 4.0
2021-05-14T14:27:41.980Z
Thanks Ben. That’s super helpful. See my **Update** above.
Jonathan Schofield
CC BY-SA 4.0
2021-05-03T18:38:43.503Z
2021-05-14T20:40:35.513Z
2021-05-14T20:40:35.513Z
Ben Croker
<p>I have a scenario where level 1 <code>structure</code> entries each have several to tens of level 2 child entries, and several to tens of entries in a different <code>channel</code> which are also allowed to be related to the level 1 <code>structure</code> entry.</p>
<pre><code>'events' structure
  - event entry 'e'
    - child 1
    - child n
'articles' channel
  - article 1
  - article n: 
      could be related to event 'e'
'people' channel
  - person 1 
  - person n: 
      could be related to a child of 'e' via a Matrix field
      and/or an article entry related to 'e' via a Matrix field
</code></pre>
<p>For each of the <code>child</code> entries and the <code>article</code> entries, there are Matrix fields in which one or more <code>people</code> entries can be related.</p>
<p>Other than looping through the set of children of 'e' and the set of related articles, and then through each of their Matrix rows to fetch their corresponding people entries, is there a cleverly more efficient way to get <strong>all the people entries used for event 'e'</strong>?</p>
<hr />
<h2>Update (revised)</h2>
<p>Thanks, Ben, for pointing the way. Initially I couldn't get this to work but then I spotted my error. Here’s what now works a treat for the following specifics…</p>
<p>I have my child pages as <code>sessions</code> where:</p>
<ul>
<li>Each child <code>session</code> has a Matrix field called <code>eventProgramme</code>
<ul>
<li>There are two <code>Entries</code> fields defined in this Matrix, one called <code>speakers</code> and one called <code>chairs</code>.</li>
</ul>
</li>
</ul>
<p>This works:</p>
<pre><code>{% set people = craft.entries.section('people').relatedTo({
    sourceElement: sessions,
    field: 'eventProgramme'
})
.all() %}
</code></pre>
<ul>
<li>It's cool that <code>sourceElement</code> can be an array such as <code>sessions</code>.</li>
<li>If I want to target a specific field I can just use something like <code>field: 'eventProgramme.speakers'</code></li>
</ul>
<p>One thing I don't understand is the distinction between <code>targetElement</code> and <code>sourceElement</code>. Only the latter works in this context.</p>
<h2>Update 2</h2>
<p>Doh! The difference between source and target element is <a href="https://craftcms.com/docs/3.x/relations.html#terminology" rel="nofollow noreferrer">clearly explained in the Docs</a> but I had missed it:</p>
<blockquote>
<ul>
<li>&quot;The <strong>source</strong> has the relational field where other elements are chosen.&quot;</li>
<li>&quot;The <strong>target</strong> element is the one selected by the source&quot;</li>
</ul>
</blockquote>

<matrix><elementquery><relationships>
Efficient way to get all related entries assigned to matrix fields across a set of entries
# Efficient way to get all related entries assigned to matrix fields across a set of entries

(Asked by: username_0 on 2021-05-03)

I have a scenario where level 1 structure entries each have several to tens of level 2 child entries, and several to tens of entries in a different channel which are also allowed to be related to the level 1 structure entry.
'events' structure
  - event entry 'e'
    - child 1
    - child n
'articles' channel
  - article 1
  - article n: 
      could be related to event 'e'
'people' channel
  - person 1 
  - person n: 
      could be related to a child of 'e' via a Matrix field
      and/or an article entry related to 'e' via a Matrix field

For each of the child entries and the article entries, there are Matrix fields in which one or more people entries can be related.
Other than looping through the set of children of 'e' and the set of related articles, and then through each of their Matrix rows to fetch their corresponding people entries, is there a cleverly more efficient way to get all the people entries used for event 'e'?

Update (revised)
Thanks, Ben, for pointing the way. Initially I couldn't get this to work but then I spotted my error. Here’s what now works a treat for the following specifics…
I have my child pages as sessions where:

Each child session has a Matrix field called eventProgramme

There are two Entries fields defined in this Matrix, one called speakers and one called chairs.



This works:
{% set people = craft.entries.section('people').relatedTo({
    sourceElement: sessions,
    field: 'eventProgramme'
})
.all() %}


It's cool that sourceElement can be an array such as sessions.
If I want to target a specific field I can just use something like field: 'eventProgramme.speakers'

One thing I don't understand is the distinction between targetElement and sourceElement. Only the latter works in this context.
Update 2
Doh! The difference between source and target element is clearly explained in the Docs but I had missed it:


"The source has the relational field where other elements are chosen."
"The target element is the one selected by the source"

**Tags:** matrix, elementquery, relationships


---

## 1 answers

### Answer by username_1 on 2021-05-03. Score: 3 | Accepted answer

If you are trying to get all people entries related to the event entry itself (what you call event "e") then I would suggest going through matrix as follows.
{% set people = craft.entries.section('people').relatedTo([
    'or',
    { targetElement: event, field: 'matrixFieldHandle.relatedEvents' },
    { targetElement: event, field: 'matrixFieldHandle.relatedArticles' }
]) %}

If you need the target element to be all children of the event then this might do the trick.
{% set eventChildren = event.children %}

{% set people = craft.entries.section('people').relatedTo([
    'or',
    { targetElement: eventChildren, field: 'matrixFieldHandle.relatedEvents' },
    { targetElement: eventChildren, field: 'matrixFieldHandle.relatedArticles' }
]) %}

You can also combine the event with its children, if necessary, as follows.
{% set eventWithChildren = event.children|push(event) %}

> **Comments:**
> **username_0**: Thanks Ben. That’s super helpful. See my **Update** above.  

---

