Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-30T16:25:26.300Z
Answers.LastActivityDate: 2024-05-17T04:16:52.633Z
Answers.LastEditDate: 2024-05-17T04:16:52.633Z
Answers.UserDisplayName: Ben Croker
Body: <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>
  
Tags: <plugin-development><filter><ordering><order><filtering>
Title: How to order or filter entries by their author's custom field?
ThreadText: # 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:
  
  ```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:
  
  ```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:
  
  ```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:
  
  ```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:
  
  ```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:
  
  ```php
  $elementQuery = Entry::find()
      ->section('sectionHandle')
      ->with(['author'])
      ->innerJoin('{{%content}} author_content', '[[author_content.elementId]] = [[authorId]]')
      ->andWhere(['author_content.field_customFieldHandle' => 'X']);
  
  ```
  
  
  ---
  
  