<p>This requires a bit of extra work on the ElementQuery, but it can be efficiently done in the same query.</p>
<p>We first perform an inner join on the author's content table using the <code>authorId</code> so that the custom field is available to us.</p>
<p>in Twig:</p>
<pre class="lang-twig prettyprint-override"><code>{% set elementQuery = craft.entries()
    .section('sectionHandle')
    .with(['author'])
    .innerJoin('{{%content}} author_content', '[[author_content.elementId]] = [[authorId]]')
    .andWhere({'author_content.field_customFieldHandle': 'X'})
%}
</code></pre>
<p>in PHP:</p>
<pre class="lang-php prettyprint-override"><code>$elementQuery = Entry::find()
    -&gt;section('sectionHandle')
    -&gt;with(['author'])
    -&gt;innerJoin('{{%content}} author_content', '[[author_content.elementId]] = [[authorId]]')
    -&gt;andWhere(['author_content.field_customFieldHandle' =&gt; 'X']);
</code></pre>
<hr />
<p>To order by a custom field we use <code>orderBy</code>.</p>
<p>in Twig:</p>
<pre class="lang-twig prettyprint-override"><code>{% set elementQuery = craft.entries()
    .section('sectionHandle')
    .with(['author'])
    .innerJoin('{{%content}} author_content', '[[author_content.elementId]] = [[authorId]]')
    .orderBy('author_content.field_customFieldHandle asc')
%}
</code></pre>
<p>in PHP:</p>
<pre class="lang-php prettyprint-override"><code>$elementQuery = Entry::find()
    -&gt;section('sectionHandle')
    -&gt;with(['author'])
    -&gt;innerJoin('{{%content}} author_content', '[[author_content.elementId]] = [[authorId]]')
    -&gt;orderBy('author_content.field_customFieldHandle asc');
</code></pre>
<hr />
<p>To filter by a custom field we add <code>andWhere</code>.</p>
<p>in Twig:</p>
<pre class="lang-twig prettyprint-override"><code>{% set elementQuery = craft.entries()
    .section('sectionHandle')
    .with(['author'])
    .innerJoin('{{%content}} author_content', '[[author_content.elementId]] = [[authorId]]')
    .andWhere({'author_content.field_customFieldHandle': 'X'})
%}
</code></pre>
<p>in PHP:</p>
<pre class="lang-php prettyprint-override"><code>$elementQuery = Entry::find()
    -&gt;section('sectionHandle')
    -&gt;with(['author'])
    -&gt;innerJoin('{{%content}} author_content', '[[author_content.elementId]] = [[authorId]]')
    -&gt;andWhere(['author_content.field_customFieldHandle' =&gt; 'X']);
</code></pre>

CC BY-SA 4.0
2019-05-30T16:25:26.300Z
2024-05-17T04:16:52.633Z
2024-05-17T04:16:52.633Z
Ben Croker
<p>Is it possible to order and/or filter all entries from a section whose author has a custom field value that is set to "X"? </p>

<p>Below is the starting code.</p>

<p>in Twig:</p>

<pre><code>{% set elementQuery = craft.entries()
    .section('sectionHandle')
    .with(['author'])
%}
</code></pre>

<p>in PHP:</p>

<pre><code>$elementQuery = Entry::find()
    -&gt;section('sectionHandle')
    -&gt;with(['author']);
</code></pre>

<plugin-development><filter><ordering><order><filtering>
How to order or filter entries by their author's custom field?
# How to order or filter entries by their author's custom field?

(Asked by: username_0 on 2019-05-30)

Is it possible to order and/or filter all entries from a section whose author has a custom field value that is set to "X"? 
Below is the starting code.
in Twig:
{% set elementQuery = craft.entries()
    .section('sectionHandle')
    .with(['author'])
%}

in PHP:
$elementQuery = Entry::find()
    ->section('sectionHandle')
    ->with(['author']);

**Tags:** plugin-development, filter, ordering, order, filtering


---

## 1 answers

### Answer by username_0 on 2019-05-30. Score: 12 | Accepted answer

This requires a bit of extra work on the ElementQuery, but it can be efficiently done in the same query.
We first perform an inner join on the author's content table using the authorId so that the custom field is available to us.
in Twig:
{% set elementQuery = craft.entries()
    .section('sectionHandle')
    .with(['author'])
    .innerJoin('{{%content}} author_content', '[[author_content.elementId]] = [[authorId]]')
    .andWhere({'author_content.field_customFieldHandle': 'X'})
%}

in PHP:
$elementQuery = Entry::find()
    ->section('sectionHandle')
    ->with(['author'])
    ->innerJoin('{{%content}} author_content', '[[author_content.elementId]] = [[authorId]]')
    ->andWhere(['author_content.field_customFieldHandle' => 'X']);


To order by a custom field we use orderBy.
in Twig:
{% set elementQuery = craft.entries()
    .section('sectionHandle')
    .with(['author'])
    .innerJoin('{{%content}} author_content', '[[author_content.elementId]] = [[authorId]]')
    .orderBy('author_content.field_customFieldHandle asc')
%}

in PHP:
$elementQuery = Entry::find()
    ->section('sectionHandle')
    ->with(['author'])
    ->innerJoin('{{%content}} author_content', '[[author_content.elementId]] = [[authorId]]')
    ->orderBy('author_content.field_customFieldHandle asc');


To filter by a custom field we add andWhere.
in Twig:
{% set elementQuery = craft.entries()
    .section('sectionHandle')
    .with(['author'])
    .innerJoin('{{%content}} author_content', '[[author_content.elementId]] = [[authorId]]')
    .andWhere({'author_content.field_customFieldHandle': 'X'})
%}

in PHP:
$elementQuery = Entry::find()
    ->section('sectionHandle')
    ->with(['author'])
    ->innerJoin('{{%content}} author_content', '[[author_content.elementId]] = [[authorId]]')
    ->andWhere(['author_content.field_customFieldHandle' => 'X']);


---

