<p>I see ... thanks a lot, Mats!</p>
<p>I modified your code a little bit (thanks to ChatGPT), now it works for multiple parent ids (Craft 4):</p>
<h3>custom.php</h3>
<pre><code>[...]
'customParentIds' =&gt; [306, 556],
[...]
</code></pre>
<h3>module</h3>
<pre><code>use yii\base\Event;
use craft\fields\BaseRelationField;
use craft\events\ElementCriteriaEvent;
use craft\elements\Entry;
use Craft;

Event::on(
    BaseRelationField::class,
    BaseRelationField::EVENT_DEFINE_SELECTION_CRITERIA,
    function (ElementCriteriaEvent $event) {
        $field = $event-&gt;sender;

        // check field handle
        if ($field-&gt;handle !== 'entryFieldHandle') {
            return;
        }

        // get customParentIds
        $parentIds = Craft::$app-&gt;config-&gt;custom-&gt;customParentIds;

        // merge descendant IDs
        $descendantIds = [];
        foreach ($parentIds as $parentId) {
            $descendants = Entry::find()-&gt;descendantOf($parentId)-&gt;level(2)-&gt;ids();
            $descendantIds = array_merge($descendantIds, $descendants);
        }

        // set the ID criteria for the event
        $event-&gt;criteria['id'] = $descendantIds;
    }
);
</code></pre>

CC BY-SA 4.0
2024-08-12T08:31:03.890Z
2024-08-12T08:31:03.890Z
Marc 
<p>Parent/child (aka ancestor/descendant) relationships aren't <a href="https://craftcms.com/docs/4.x/relations.html" rel="nofollow noreferrer">relations</a>, which is what the &quot;Related to&quot; (and the <code>relatedTo</code> query param underpinning it) works with. In other words, it's not possible to create a &quot;Related to&quot; condition that does what you need it to.</p>
<p>Unfortunately, neither Craft 4 or 5 has a &quot;Descendant of&quot; condition type. Sounds like a valid feature request, though!</p>
<p>If you really want to pull this off, the only solution I can think of is to create a <a href="https://craftcms.com/docs/4.x/extend/module-guide.html" rel="nofollow noreferrer">custom Yii module</a>, with an event listener for the <a href="https://craftcms.com/docs/4.x/extend/events.html#event-code-generator" rel="nofollow noreferrer"><code>EVENT_DEFINE_SELECTION_CRITERIA</code> event</a>:</p>
<pre class="lang-php prettyprint-override"><code>use yii\base\Event;
use craft\fields\BaseRelationField;
use craft\events\ElementCriteriaEvent;

Event::on(
    BaseRelationField::class,
    BaseRelationField::EVENT_DEFINE_SELECTION_CRITERIA,
    function (ElementCriteriaEvent $event) {
        $field = $event-&gt;sender;
        if ($field-&gt;handle !== 'entryFieldHandle') {
            return;
        }
        $event-&gt;criteria['descendantOf'] = 306;
    }
);
</code></pre>
<p>Finally, as general advice you should avoid hard coding entry IDs anywhere in your build. Entry IDs will typically vary between environments, so a better approach is to either use environment variables, or an entry query to find the proper entry without depending on a hard coded ID.</p>

CC BY-SA 4.0
2024-08-12T09:46:54.487Z
Added a feature request: https://github.com/craftcms/cms/discussions/15518
Marc 
CC BY-SA 4.0
2024-08-11T19:31:17.707Z
2024-08-11T19:31:17.707Z
Mats Mikkel Rummelhoff
<p>See: <a href="https://craftcms.com/docs/4.x/entries.html#descendantof" rel="nofollow noreferrer">https://craftcms.com/docs/4.x/entries.html#descendantof</a></p>
<p>I want to create a field which allows to select all (direct) <strong>descendants</strong> of a certain <strong>entry</strong> (id=306), section type = <strong>structure</strong>.</p>
<p>My solution: <code>{craft.entries().descendantOf(306).all()}</code></p>
<p>Result: I got <strong>all</strong> entries ... not only the wanted descendants ... but why?<br />
I've no idea ... it should work - but it does not.</p>
<p>Further testing and tryout: A dump in a twig template works fine:<br />
<code>{% dump(craft.entries().descendantOf(306).all()) %}</code></p>
<h3>Screenshots</h3>
<h4>Field configuration</h4>
<p><a href="https://i.sstatic.net/H3Z0XbbO.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/H3Z0XbbO.png" alt="enter image description here" /></a></p>
<h4>Section</h4>
<p><a href="https://i.sstatic.net/9QTtR6fK.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/9QTtR6fK.png" alt="enter image description here" /></a></p>
<h4>wrong result -&gt; too many entries</h4>
<p><a href="https://i.sstatic.net/M5NcbfpB.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/M5NcbfpB.png" alt="enter image description here" /></a></p>

<field><craft4>
Field: Selectable Entries Condition -> get all descendants of certain id
# Field: Selectable Entries Condition -> get all descendants of certain id

(Asked by: username_0 on 2024-08-11)

See: <https://craftcms.com/docs/4.x/entries.html#descendantof>

I want to create a field which allows to select all (direct) **descendants** of a certain **entry** (id=306), section type = **structure**.

My solution: `{craft.entries().descendantOf(306).all()}`

Result: I got **all** entries ... not only the wanted descendants ... but why?  
I've no idea ... it should work - but it does not.

Further testing and tryout: A dump in a twig template works fine:  
`{% dump(craft.entries().descendantOf(306).all()) %}`

### Screenshots

#### Field configuration

[![enter image description here](https://i.sstatic.net/H3Z0XbbO.png)](https://i.sstatic.net/H3Z0XbbO.png)

#### Section

[![enter image description here](https://i.sstatic.net/9QTtR6fK.png)](https://i.sstatic.net/9QTtR6fK.png)

#### wrong result -> too many entries

[![enter image description here](https://i.sstatic.net/M5NcbfpB.png)](https://i.sstatic.net/M5NcbfpB.png)

**Tags:** field, craft4


---

## 2 answers

### Answer by username_0 on 2024-08-12. Score: 1 | Accepted answer

I see ... thanks a lot, Mats!
I modified your code a little bit (thanks to ChatGPT), now it works for multiple parent ids (Craft 4):
custom.php
[...]
'customParentIds' => [306, 556],
[...]

module
use yii\base\Event;
use craft\fields\BaseRelationField;
use craft\events\ElementCriteriaEvent;
use craft\elements\Entry;
use Craft;

Event::on(
    BaseRelationField::class,
    BaseRelationField::EVENT_DEFINE_SELECTION_CRITERIA,
    function (ElementCriteriaEvent $event) {
        $field = $event->sender;

        // check field handle
        if ($field->handle !== 'entryFieldHandle') {
            return;
        }

        // get customParentIds
        $parentIds = Craft::$app->config->custom->customParentIds;

        // merge descendant IDs
        $descendantIds = [];
        foreach ($parentIds as $parentId) {
            $descendants = Entry::find()->descendantOf($parentId)->level(2)->ids();
            $descendantIds = array_merge($descendantIds, $descendants);
        }

        // set the ID criteria for the event
        $event->criteria['id'] = $descendantIds;
    }
);


---

### Answer by username_1 on 2024-08-11. Score: 2

Parent/child (aka ancestor/descendant) relationships aren't relations, which is what the "Related to" (and the relatedTo query param underpinning it) works with. In other words, it's not possible to create a "Related to" condition that does what you need it to.
Unfortunately, neither Craft 4 or 5 has a "Descendant of" condition type. Sounds like a valid feature request, though!
If you really want to pull this off, the only solution I can think of is to create a custom Yii module, with an event listener for the EVENT_DEFINE_SELECTION_CRITERIA event:
use yii\base\Event;
use craft\fields\BaseRelationField;
use craft\events\ElementCriteriaEvent;

Event::on(
    BaseRelationField::class,
    BaseRelationField::EVENT_DEFINE_SELECTION_CRITERIA,
    function (ElementCriteriaEvent $event) {
        $field = $event->sender;
        if ($field->handle !== 'entryFieldHandle') {
            return;
        }
        $event->criteria['descendantOf'] = 306;
    }
);

Finally, as general advice you should avoid hard coding entry IDs anywhere in your build. Entry IDs will typically vary between environments, so a better approach is to either use environment variables, or an entry query to find the proper entry without depending on a hard coded ID.

> **Comments:**
> **username_0**: Added a feature request: https://github.com/craftcms/cms/discussions/15518  

---

