Answers.Body: <p>Try this:</p>
  
  <pre><code>// Return all ids for your posts' section
  {% set postIds = craft.entries.section('blogEntries').ids() %}
  
  // Look for and return employees which have a relation with any of the posts returned in 'postIds'
  {% set writers = craft.entries.section('employee').relatedTo({ sourceElement: entryIds }).all() %}
  
  // Loop and display
  {% for writer in writers %}
      {{ writer.title }} &lt;br&gt;
  {% endfor %}
  </code></pre>
  
  <p>This will only return a list of writers who have written a post, not a list of all writers but I guess that's what you want..?</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-22T10:27:46.277Z
Answers.Text: This worked like a charm and it's so obviously the way to go about it. Really impressed with how powerful the relatedTo method is as well! Thanks!
Answers.UserDisplayName: Kristoffer Zaar
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-14T23:50:31.930Z
Answers.LastActivityDate: 2019-01-14T23:55:34.100Z
Answers.LastEditDate: 2019-01-14T23:55:34.100Z
Answers.UserDisplayName: Oli
Body: <p>I'm trying to build a filter for a blog section in Craft 3. </p>
  
  <p>I'm fairly new to Craft and my client now wants a dropdown filter where they can filter out authors. Thus, I want to write a query that fetches a unique list of authors from my blog channel structure.</p>
  
  <p>The structure is: 
  section: <code>blogEntries</code> (Channel) with an entry type <code>blogEntry</code>. </p>
  
  <p>Each <code>blogEntry</code> has a <code>content</code> (matrix) and and <code>writer</code> entry field which is connected to one or more <code>employee</code> entries.</p>
  
  <p>So; <code>blogEntries &gt; blogEntry &gt; writer(s) &gt; employee</code></p>
  
  <p>What I want is something like </p>
  
  <pre><code>craft.entries.section('blogEntries').column('writer').distinct().all()
  </code></pre>
  
  <p>This is what I have:</p>
  
  <pre><code>{# Way too slow #}
  {% for item in craft.entries.section('blogEntries').all() %}
      {% if item.writer|length %}
          {% set person = item.writer.one() %}
          {% set name =  person.firstname ~ (person.surname|length ? ' ' ~ person.surname : '') %}
          {% if name not in writers %}
              {% set writers = writers|merge([name]) %}
          {% endif %}
      {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>Terrible solution, terrible performance. </p>
  
  <p>How can I improve this code? Any ideas would be appreciated.</p>
  
Tags: <craft3><querystring>
Title: Template query: Advanced section query to get unique items from custom entry type field
ThreadText: # Template query: Advanced section query to get unique items from custom entry type field
  
  (Asked by: username_0 on 2019-01-14)
  
  I'm trying to build a filter for a blog section in Craft 3.
  
  I'm fairly new to Craft and my client now wants a dropdown filter where they can filter out authors. Thus, I want to write a query that fetches a unique list of authors from my blog channel structure.
  
  The structure is:
  section: `blogEntries` (Channel) with an entry type `blogEntry`.
  
  Each `blogEntry` has a `content` (matrix) and and `writer` entry field which is connected to one or more `employee` entries.
  
  So; `blogEntries > blogEntry > writer(s) > employee`
  
  What I want is something like
  
  ```
  craft.entries.section('blogEntries').column('writer').distinct().all()
  
  ```
  
  This is what I have:
  
  ```
  {# Way too slow #}
  {% for item in craft.entries.section('blogEntries').all() %}
      {% if item.writer|length %}
          {% set person = item.writer.one() %}
          {% set name =  person.firstname ~ (person.surname|length ? ' ' ~ person.surname : '') %}
          {% if name not in writers %}
              {% set writers = writers|merge([name]) %}
          {% endif %}
      {% endif %}
  {% endfor %}
  
  ```
  
  Terrible solution, terrible performance.
  
  How can I improve this code? Any ideas would be appreciated.
  
  **Tags:** craft3, querystring
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-01-14. Score: 0 | Accepted answer
  
  Try this:
  
  ```
  // Return all ids for your posts' section
  {% set postIds = craft.entries.section('blogEntries').ids() %}
  
  // Look for and return employees which have a relation with any of the posts returned in 'postIds'
  {% set writers = craft.entries.section('employee').relatedTo({ sourceElement: entryIds }).all() %}
  
  // Loop and display
  {% for writer in writers %}
      {{ writer.title }} <br>
  {% endfor %}
  
  ```
  
  This will only return a list of writers who have written a post, not a list of all writers but I guess that's what you want..?
  
  > **Comments:**
  > **username_0**: This worked like a charm and it's so obviously the way to go about it. Really impressed with how powerful the relatedTo method is as well! Thanks!  
  
  ---
  
  