<p>The <code>with</code> parameter is used for <a href="https://craftcms.com/docs/3.x/dev/eager-loading-elements.html" rel="nofollow noreferrer">eager-loading</a> to reduce the amount of queries your template needs to execute, but it doesn't apply any filtering. If you want to limit your query to entries that have at least one related entry in the <code>relatedEntries</code> field, you can use the <a href="https://craftcms.com/docs/3.x/entries-fields.html#querying-elements-with-entries-fields" rel="nofollow noreferrer"><code>:notempty:</code> syntax</a>:</p>
<pre class="lang-php prettyprint-override"><code>$paidFeatures = Entry::find()
            -&gt;section('paidFeatures')
            -&gt;type(['simpleFeature', 'customFeature'])
            -&gt;with(['relatedEntries'])
            -&gt;relatedEntries(':notempty:')
            -&gt;status(['expired', 'disabled'])
            -&gt;limit(10)
            -&gt;all();
</code></pre>
<p>If you want to limit the query to entries related to a specific set of other entries, you can pass in an entry query, array of entries or entry IDs (see the documentation linked above for supported formats):</p>
<pre class="lang-php prettyprint-override"><code>$awesomeFeatures = Entry::find()
            -&gt;section('paidFeatures')
            // add whatever conditions you want here
            -&gt;type('awesome_features');

$paidFeatures = Entry::find()
            -&gt;section('paidFeatures')
            -&gt;type(['simpleFeature', 'customFeature'])
            -&gt;with(['relatedEntries'])
            -&gt;relatedEntries($awesomeFeatures)
            -&gt;status(['expired', 'disabled'])
            -&gt;limit(10)
            -&gt;all();
</code></pre>

CC BY-SA 4.0
2022-02-02T15:20:42.807Z
I think the `->relatedEntries(':notempty:')` is what I needed. Giving it a go now. Will report back.
Sean Delaney
CC BY-SA 4.0
2022-02-02T16:05:26.790Z
Thanks @MoritzLost, that worked with a few tweaks. Now that you pointed me to the correct query setup, I remember seeing `:notempty:` in the docs but it didn't click. TBH I probably spent too long looking at the code, trying to resolve it myself that I started to overlook stuff. Thanks again!
Sean Delaney
CC BY-SA 4.0
2022-02-02T16:14:55.097Z
@SeanDelaney Yeah, some of those methods are really well hidden in the docs. Glad you got it working!
MoritzLost
CC BY-SA 4.0
2022-02-02T15:13:31.190Z
2022-02-02T15:13:31.190Z
MoritzLost
<p>I have a simple query:</p>
<pre><code>$paidFeatures = Entry::find()
            -&gt;section('paidFeatures')
            -&gt;type(['simpleFeature', 'customFeature'])
            -&gt;with(['relatedEntries'])
            -&gt;status(['expired', 'disabled'])
            -&gt;limit(10);
</code></pre>
<p>Some of the entries do not have <code>relatedEntries</code> linked/relationship setup.</p>
<p>I only want <code>paidFeatures</code> with linked/related <code>relatedEntries</code> ? So basically any entry in my paidFeatures list will always show a <code>relatedEntries</code>. No blanks.</p>
<p>I'm struggling to get my query to further filter linked/related <code>relatedEntries</code>.</p>
<p><strong>UPDATE 1:</strong></p>
<p>I've also tried this but doesnt give me what I want either:</p>
<pre><code>$paidFeatures = Entry::find()
            -&gt;section('paidFeatures')
            -&gt;type(['simpleFeature', 'customFeature'])
            //-&gt;with(['relatedEntries'])
            //-&gt;relatedEntries('status:live')
            -&gt;status(['expired', 'disabled'])
            -&gt;all();

        $paidFeaturesWithRelatedEntries = Entry::find()
            -&gt;section('ideas')
            -&gt;relatedTo($paidFeatures)
            -&gt;all();
</code></pre>
<p>How can I do this?</p>

<relations>
Query entries "with" entries that are actually related
# Query entries "with" entries that are actually related

(Asked by: username_0 on 2022-02-02)

I have a simple query:
$paidFeatures = Entry::find()
            ->section('paidFeatures')
            ->type(['simpleFeature', 'customFeature'])
            ->with(['relatedEntries'])
            ->status(['expired', 'disabled'])
            ->limit(10);

Some of the entries do not have relatedEntries linked/relationship setup.
I only want paidFeatures with linked/related relatedEntries ? So basically any entry in my paidFeatures list will always show a relatedEntries. No blanks.
I'm struggling to get my query to further filter linked/related relatedEntries.
UPDATE 1:
I've also tried this but doesnt give me what I want either:
$paidFeatures = Entry::find()
            ->section('paidFeatures')
            ->type(['simpleFeature', 'customFeature'])
            //->with(['relatedEntries'])
            //->relatedEntries('status:live')
            ->status(['expired', 'disabled'])
            ->all();

        $paidFeaturesWithRelatedEntries = Entry::find()
            ->section('ideas')
            ->relatedTo($paidFeatures)
            ->all();

How can I do this?

**Tags:** relations

---

## 1 answers

### Answer by username_1 on 2022-02-02. Score: 1 | Accepted answer

The with parameter is used for eager-loading to reduce the amount of queries your template needs to execute, but it doesn't apply any filtering. If you want to limit your query to entries that have at least one related entry in the relatedEntries field, you can use the :notempty: syntax:
$paidFeatures = Entry::find()
            ->section('paidFeatures')
            ->type(['simpleFeature', 'customFeature'])
            ->with(['relatedEntries'])
            ->relatedEntries(':notempty:')
            ->status(['expired', 'disabled'])
            ->limit(10)
            ->all();

If you want to limit the query to entries related to a specific set of other entries, you can pass in an entry query, array of entries or entry IDs (see the documentation linked above for supported formats):
$awesomeFeatures = Entry::find()
            ->section('paidFeatures')
            // add whatever conditions you want here
            ->type('awesome_features');

$paidFeatures = Entry::find()
            ->section('paidFeatures')
            ->type(['simpleFeature', 'customFeature'])
            ->with(['relatedEntries'])
            ->relatedEntries($awesomeFeatures)
            ->status(['expired', 'disabled'])
            ->limit(10)
            ->all();

> **Comments:**
> **username_0**: I think the `->relatedEntries(':notempty:')` is what I needed. Giving it a go now. Will report back.  
> **username_0**: Thanks @MoritzLost, that worked with a few tweaks. Now that you pointed me to the correct query setup, I remember seeing `:notempty:` in the docs but it didn't click. TBH I probably spent too long looking at the code, trying to resolve it myself that I started to overlook stuff. Thanks again!  
> **username_1**: @SeanDelaney Yeah, some of those methods are really well hidden in the docs. Glad you got it working!  

---

