<p>If the authors are the source of the relationship, you can simply use the <code>:notempty:</code> syntax on the field with the relation. But I'm going to assume the authors are the target of the relationship, since that probably makes more sense.</p>
<p>In this case, you can solve this with a custom where condition or join, but that's a bit more involved. A simpler solution is to pass a query that selects all applicable entries to the <code>relatedTo</code> parameter.</p>
<pre class="lang-twig prettyprint-override"><code>{% set articles = craft.entries().section('article') %}
{% set authors = craft.entries()
    .section('authors')
    .relatedTo(articles)
    .all()
%}
</code></pre>

CC BY-SA 4.0
2022-03-17T14:12:17.660Z
Oh, cool. I did not know that I could relate to an entire section like that. Thanks!
InanisAtheos
CC BY-SA 4.0
2022-03-17T13:56:40.733Z
2022-03-17T13:56:40.733Z
MoritzLost
<p>I have a channel with Authors. Not all of them are used right now, so I'm trying to construct a query that will fetch all Authors are are related to <em>some</em> article.</p>
<p>This is what I've tried so far:</p>
<pre><code>{% set allAuthors = craft.entries
    .section('authors')
    .relatedTo('not ' ~ null)
    .limit(null)
    .all()
%}
</code></pre>
<p>This yielded exactly nothing. Same with having 'not null' as a string on line 3 above.</p>
<p>I've tried looking over the documentation <a href="https://craftcms.com/docs/3.x/relations.html" rel="nofollow noreferrer">here</a>, but was unable to find information on this scenario.</p>
<p>This is the (extremely) dirty way of accomplishing this, but pagination becomes nearly impossible:</p>
<pre><code>{% set allAuthors = craft.entries
    .section('authors')
    .limit(null)
    .all()
%}
&lt;ul&gt;
{% for author in allAuthors %}
    &lt;li&gt;
    {% set articlesByThisAuthor = craft.entries
        .section('articles')
        .relatedTo(author)
        .all()
    %}
    {% if articlesByThisAuthor|length &gt; 0 %}
        {{ author.title }}: {{ articlesByThisAuthor|length }}
    {% endif %}
    &lt;/li&gt;
{% endfor %}
</code></pre>
<p>Perhaps I'm approaching this all wrong?</p>

<relations><relatedto>
Fetching entries that are related to 'not null'
# Fetching entries that are related to 'not null'

(Asked by: username_0 on 2022-03-17)

I have a channel with Authors. Not all of them are used right now, so I'm trying to construct a query that will fetch all Authors are are related to some article.
This is what I've tried so far:
{% set allAuthors = craft.entries
    .section('authors')
    .relatedTo('not ' ~ null)
    .limit(null)
    .all()
%}

This yielded exactly nothing. Same with having 'not null' as a string on line 3 above.
I've tried looking over the documentation here, but was unable to find information on this scenario.
This is the (extremely) dirty way of accomplishing this, but pagination becomes nearly impossible:
{% set allAuthors = craft.entries
    .section('authors')
    .limit(null)
    .all()
%}

{% for author in allAuthors %}
    
    {% set articlesByThisAuthor = craft.entries
        .section('articles')
        .relatedTo(author)
        .all()
    %}
    {% if articlesByThisAuthor|length > 0 %}
        {{ author.title }}: {{ articlesByThisAuthor|length }}
    {% endif %}
    
{% endfor %}

Perhaps I'm approaching this all wrong?

**Tags:** relations, relatedto


---

## 1 answers

### Answer by username_1 on 2022-03-17. Score: 2 | Accepted answer

If the authors are the source of the relationship, you can simply use the :notempty: syntax on the field with the relation. But I'm going to assume the authors are the target of the relationship, since that probably makes more sense.
In this case, you can solve this with a custom where condition or join, but that's a bit more involved. A simpler solution is to pass a query that selects all applicable entries to the relatedTo parameter.
{% set articles = craft.entries().section('article') %}
{% set authors = craft.entries()
    .section('authors')
    .relatedTo(articles)
    .all()
%}

> **Comments:**
> **username_0**: Oh, cool. I did not know that I could relate to an entire section like that. Thanks!  

---

