<p>Your code looks correct – this query will match all news that are related to any of the children of the current entry. The only thing that might be causing issues is that it will find <em>all</em> news that are related to <em>any</em> of the children in <em>any</em> field, and regardless of which element is the source of target. You can further limit your query to a specific field and source/target relation like this:</p>
<pre class="lang-twig prettyprint-override"><code>{% set children = entry.children().ids() %}
{% set relatedEntries = craft.entries()
    .section('news')
    .relatedTo({
        targetElement: children,
        field: 'myEntriesField',
    })
    .all()
%}
</code></pre>
<p>Replace <code>myEntriesField</code> with the handle of the field in your <code>news</code> section that is used to select the child entries.</p>
<hr />
<p>Another option to better address this: In Craft 4.4+ entries fields have a <a href="https://github.com/craftcms/cms/pull/11749" rel="nofollow noreferrer">'Maintain Hierarchy' setting</a>. This will cause the field to always select all ancestors of the selected child as well. This way, the parent entry of the selected child entry will always be selected as well, and will be directly related to the news entry. Then you can just filter by news that are related to the current (parent) entry.</p>

CC BY-SA 4.0
2023-07-07T11:52:46.710Z
Thank you very much Moritz, it works now with the addition of a curly bracket at the end of the relatedTo.
Marcel
CC BY-SA 4.0
2023-07-07T12:54:44.097Z
@Marcel Oops, you're right. Just fixed that.
MoritzLost
CC BY-SA 4.0
2023-07-07T10:29:50.407Z
2023-07-07T12:54:17.027Z
2023-07-07T12:54:17.027Z
MoritzLost
<p>I use a structure section with two levels, parents and children. Is it possible to show related entries of a parent, where only a child of that parent is selected as a related entry?</p>
<p>I did try to solve it like this:</p>
<pre><code>{% set children = entry.children().all() %}
{% set relatedEntries = craft.entries.section('news').relatedTo(children).limit(3)%}
</code></pre>
<p>But parents now show all entries, not only the entries that are related to (one of) their children.</p>
<p>I  would like to create something like this:</p>
<pre><code>**Structure:**
Parent1
-&gt; Child11
-&gt; Child12
-&gt; Child13
Parent2
-&gt; Child21
-&gt; Child22

**News entry**
-&gt; News1 with related entry Child11

**In the Parent template**
News1 is related to Parent1

</code></pre>

CC BY-SA 4.0
2023-07-07T09:08:27.797Z
Where do you want to show the related entries, on the parent template? So the `entry` variable in your code is the parent? And what are the children related to, in which field? On the parent or the child?
MoritzLost
CC BY-SA 4.0
2023-07-07T10:01:52.707Z
Hi Moritz, Yes the entry variable is the parent (in the parent template). And the children of the parent are selected in the entries  of the section 'news'.
Marcel
<relations><structure>
Show related entries of a parent, where only a child of that parent is selected as a related entry
# Show related entries of a parent, where only a child of that parent is selected as a related entry

(Asked by: username_0 on 2023-07-07)

I use a structure section with two levels, parents and children. Is it possible to show related entries of a parent, where only a child of that parent is selected as a related entry?
I did try to solve it like this:
{% set children = entry.children().all() %}
{% set relatedEntries = craft.entries.section('news').relatedTo(children).limit(3)%}

But parents now show all entries, not only the entries that are related to (one of) their children.
I  would like to create something like this:
**Structure:**
Parent1
-> Child11
-> Child12
-> Child13
Parent2
-> Child21
-> Child22

**News entry**
-> News1 with related entry Child11

**In the Parent template**
News1 is related to Parent1

**Tags:** relations, structure
> **Comments:**
> **username_1**: Where do you want to show the related entries, on the parent template? So the `entry` variable in your code is the parent? And what are the children related to, in which field? On the parent or the child?  
> **username_0**: Hi Moritz, Yes the entry variable is the parent (in the parent template). And the children of the parent are selected in the entries  of the section 'news'.  

---

## 1 answers

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

Your code looks correct – this query will match all news that are related to any of the children of the current entry. The only thing that might be causing issues is that it will find all news that are related to any of the children in any field, and regardless of which element is the source of target. You can further limit your query to a specific field and source/target relation like this:
{% set children = entry.children().ids() %}
{% set relatedEntries = craft.entries()
    .section('news')
    .relatedTo({
        targetElement: children,
        field: 'myEntriesField',
    })
    .all()
%}

Replace myEntriesField with the handle of the field in your news section that is used to select the child entries.

Another option to better address this: In Craft 4.4+ entries fields have a 'Maintain Hierarchy' setting. This will cause the field to always select all ancestors of the selected child as well. This way, the parent entry of the selected child entry will always be selected as well, and will be directly related to the news entry. Then you can just filter by news that are related to the current (parent) entry.

> **Comments:**
> **username_0**: Thank you very much Moritz, it works now with the addition of a curly bracket at the end of the relatedTo.  
> **username_1**: @Marcel Oops, you're right. Just fixed that.  

---

