Answers.Body: <p>You could do something like this:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{# Fetch entries + authors and group by author id #}
  {% set entriesByAuthors = craft
      .entries()
      .section('itbuzz')
      .limit(null)
      .with('author')
      .all() | group(e =&gt; e.author.id) %}
  
  {# Build an array with authorId and entry count for each that we can sort them #}
  {% set leaderBoard = [] %}
  {% for author, entries in entriesByAuthors  %}
      {% set leaderBoard = leaderBoard | merge([{'author' : author, 'count' : entries | length}]) %}
  {% endfor %}
  
  {# Build an array of user ids only sorted by entry count and extract the top 8 #}
  {% set authorIds = [] %}
  {% for result in leaderBoard | multisort(e =&gt; e.count, direction=SORT_DESC) | slice(0,8) %}
      {% set authorIds = authorIds | merge([result.author]) %}
  {% endfor %}
  
  {# Fetch users based on our sorted and sliced array of ids #}
  {% for author in craft.users().id(authorIds).all() %}
      {{ author.firstName }} - {{ author.lastName }}&lt;br&gt;
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-04T14:36:26.753Z
Answers.Text: Oli, this works perfectly, thank you.  I"m not sure what's going on in with `.with(author)` and the `|group` filter. Could you explain that a little for me. Thank you.
Answers.UserDisplayName: CreateSean
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-04T09:47:21.007Z
Answers.LastActivityDate: 2020-03-04T09:47:21.007Z
Answers.UserDisplayName: Oli
Answers.Body: <p>Maybe a good excuse to use the spaceship operator (<code>&lt;=&gt;</code>)... Combined with the <code>group</code> filter, this seems to work for me:</p>
  
  <pre><code>{% set groupedEntries = craft.entries.section('itbuzz').all()|group('author') %}
  
  {% for author, entries in groupedEntries|sort((a, b) =&gt; a|length &lt;=&gt; b|length)|reverse|slice(0,8) %}
      &lt;h3&gt;{{ author }}&lt;/h3&gt;
      {# optionally output their entries too... #}
      &lt;ul&gt;
          {% for entry in entries %}
              &lt;li&gt;{{ entry.title }}&lt;/li&gt;
          {% endfor %}
      &lt;/ul&gt;
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-04T10:05:27.133Z
Answers.Text: Thought of that but the simple fact of grouping means you loose your `user` object and Sean seems to need to access `author.stuff` ¯\_(ツ)_/¯
Answers.UserDisplayName: Oli
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-04T10:06:33.800Z
Answers.Text: Ah ha good point yerp.
Answers.UserDisplayName: James Smith
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-04T14:38:13.550Z
Answers.Text: That sort filter - wow, I would never have thought of that.
Answers.UserDisplayName: CreateSean
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-04T10:00:15.277Z
Answers.LastActivityDate: 2020-03-04T10:00:15.277Z
Answers.UserDisplayName: James Smith
Body: <p>I need to output a list of channel authors. There are about 40 authors but I want the 8 authors with the most entries. Not sure how to achieve this.</p>
  
  <p>What I have so far:</p>
  
  <pre><code>    {# get all the itbuzz entries #}
      {% set allEntries = craft.entries.section('itbuzz').all() %}
      {# get all the authors #}
      {% set authors = craft.users.group('itBuzzAuthor').limit(8).all() %}
  
      &lt;ul class="flex flex-row flex-wrap"&gt;
        {% for author in authors %}
          &lt;li class="inline-block mb-4 mr-4 text-sm text-center"&gt;
            &lt;img src="{{ craft.gravatar.url( author.email ) }}" alt=""&gt;
            &lt;a href="#" class="text-blackBrand"&gt;{{ author.firstName }}&lt;br&gt;{{ author.lastName }}&lt;/a&gt;
          &lt;/li&gt;
        {% endfor %}
      &lt;/ul&gt;
  </code></pre>
  
  <p>All this does is output the first 8 authors, it's not finding the 8 with the most entries.</p>
  
Tags: <templating><users><author>
Title: List authors on front end by total number of entries
ThreadText: # List authors on front end by total number of entries
  
  (Asked by: username_0 on 2020-03-02)
  
  I need to output a list of channel authors. There are about 40 authors but I want the 8 authors with the most entries. Not sure how to achieve this.
  
  What I have so far:
  
  ```
      {# get all the itbuzz entries #}
      {% set allEntries = craft.entries.section('itbuzz').all() %}
      {# get all the authors #}
      {% set authors = craft.users.group('itBuzzAuthor').limit(8).all() %}
  
      <ul class="flex flex-row flex-wrap">
        {% for author in authors %}
          <li class="inline-block mb-4 mr-4 text-sm text-center">
            <img src="{{ craft.gravatar.url( author.email ) }}" alt="">
            <a href="#" class="text-blackBrand">{{ author.firstName }}<br>{{ author.lastName }}</a>
          </li>
        {% endfor %}
      </ul>
  
  ```
  
  All this does is output the first 8 authors, it's not finding the 8 with the most entries.
  
  **Tags:** templating, users, author
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-03-04. Score: 2 | Accepted answer
  
  You could do something like this:
  
  ```twig
  {# Fetch entries + authors and group by author id #}
  {% set entriesByAuthors = craft
      .entries()
      .section('itbuzz')
      .limit(null)
      .with('author')
      .all() | group(e => e.author.id) %}
  
  {# Build an array with authorId and entry count for each that we can sort them #}
  {% set leaderBoard = [] %}
  {% for author, entries in entriesByAuthors  %}
      {% set leaderBoard = leaderBoard | merge([{'author' : author, 'count' : entries | length}]) %}
  {% endfor %}
  
  {# Build an array of user ids only sorted by entry count and extract the top 8 #}
  {% set authorIds = [] %}
  {% for result in leaderBoard | multisort(e => e.count, direction=SORT_DESC) | slice(0,8) %}
      {% set authorIds = authorIds | merge([result.author]) %}
  {% endfor %}
  
  {# Fetch users based on our sorted and sliced array of ids #}
  {% for author in craft.users().id(authorIds).all() %}
      {{ author.firstName }} - {{ author.lastName }}<br>
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Oli, this works perfectly, thank you.  I"m not sure what's going on in with `.with(author)` and the `|group` filter. Could you explain that a little for me. Thank you.  
  
  ---
  
  ### Answer by username_2 on 2020-03-04. Score: 1
  
  Maybe a good excuse to use the spaceship operator (`<=>`)... Combined with the `group` filter, this seems to work for me:
  
  ```
  {% set groupedEntries = craft.entries.section('itbuzz').all()|group('author') %}
  
  {% for author, entries in groupedEntries|sort((a, b) => a|length <=> b|length)|reverse|slice(0,8) %}
      <h3>{{ author }}</h3>
      {# optionally output their entries too... #}
      <ul>
          {% for entry in entries %}
              <li>{{ entry.title }}</li>
          {% endfor %}
      </ul>
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_1**: Thought of that but the simple fact of grouping means you loose your `user` object and Sean seems to need to access `author.stuff` ¯\_(ツ)_/¯  
  > **username_2**: Ah ha good point yerp.  
  > **username_0**: That sort filter - wow, I would never have thought of that.  
  
  ---
  
  