<p>A plugin approach might be to create a method in your variables file that queries the database directly:</p>

<pre><code>public function usersByPostCount($limit = '10')
{
    $query = craft()-&gt;db-&gt;createCommand();
    $users = $query
        -&gt;select('craft_entries.authorId id, COUNT(*) count')
        -&gt;from('entries')
        -&gt;group('craft_entries.authorId')
        -&gt;order('count DESC')
        -&gt;limit($limit)
        -&gt;queryAll();
    return $users;
}
</code></pre>

<p>which will return an array:</p>

<pre><code>Array: [{"id":"4","count":"241"},{"id":"24","count":"2"},{"id":"171","count":"2"},{"id":null,"count":"1"}]
</code></pre>

<p>that can be accessed using:</p>

<pre><code>&lt;ul&gt;
    {% set usersByPostCount = craft.myPlugin.usersByPostCount(5) %}
    {% for data in usersByPostCount %}
        {% if data.id %}
            {% set user = craft.users.id(data.id).first %}
            {% set postCount = data.count %}
            &lt;li&gt;{{ user.firstName }} {{ user.lastName }} [{{ postCount }}]&lt;/li&gt;
        {% endif %}
    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>Note: this only returns users that are the author of at least one entry. In order to get all users you would need to join the users table into the query.</p>

CC BY-SA 3.0
2015-02-11T18:38:39.420Z
2015-02-11T19:30:56.247Z
2015-02-11T19:30:56.247Z
Douglas McDonald
<p>Here's how I did something similar, now edited the code for you to work with users. The better approach would be to do this with a plugin, because, as you can imagine, the only way doing this with Twig will end up in lots of DB queries, so please make sure to use the <code>cache</code> tag with this: </p>

<pre><code>{# Build an associative array of user IDs to the count of related entries #}
{% set userData = [] %}

{# Go through all users #}
{% for user in craft.users %}

    {# Get total number of entries this user is the author of #}
    {% set count = craft.entries.authorId(user.id).total() %}

    {# Save entries count and ALL user data we gonna use to our array #}
    {% set userData = userData|merge([{ entriesCount: count, userId: user.id, userName: user.name }]) %}

{% endfor %}

{# Sort, reverse-sort and pick top 10 elements of the array #}
{% set userData = userData|sort|reverse|slice(0, 10) %}

{# Print the top 10 list #}
{% for element in userData %}
    &lt;li&gt;{{ element.userName }} ({{ element.entriesCount }} entries)&lt;/li&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-02-11T13:34:44.087Z
2015-02-11T13:58:21.007Z
2015-02-11T13:58:21.007Z
carlcs
<p>I want to be able to display Top Authors based on their entry counts. Has anyone ever tried doing this?</p>

<templating><entries><users><author>
Get the Top Authors based on entry counts?
# Get the Top Authors based on entry counts?

(Asked by: username_0 on 2015-02-11)

I want to be able to display Top Authors based on their entry counts. Has anyone ever tried doing this?

**Tags:** templating, entries, users, author


---

## 2 answers

### Answer by username_1 on 2015-02-11. Score: 3

A plugin approach might be to create a method in your variables file that queries the database directly:
public function usersByPostCount($limit = '10')
{
    $query = craft()->db->createCommand();
    $users = $query
        ->select('craft_entries.authorId id, COUNT(*) count')
        ->from('entries')
        ->group('craft_entries.authorId')
        ->order('count DESC')
        ->limit($limit)
        ->queryAll();
    return $users;
}

which will return an array:
Array: [{"id":"4","count":"241"},{"id":"24","count":"2"},{"id":"171","count":"2"},{"id":null,"count":"1"}]

that can be accessed using:

    {% set usersByPostCount = craft.myPlugin.usersByPostCount(5) %}
    {% for data in usersByPostCount %}
        {% if data.id %}
            {% set user = craft.users.id(data.id).first %}
            {% set postCount = data.count %}
            {{ user.firstName }} {{ user.lastName }} [{{ postCount }}]
        {% endif %}
    {% endfor %}


Note: this only returns users that are the author of at least one entry. In order to get all users you would need to join the users table into the query.


---

### Answer by username_2 on 2015-02-11. Score: 3

Here's how I did something similar, now edited the code for you to work with users. The better approach would be to do this with a plugin, because, as you can imagine, the only way doing this with Twig will end up in lots of DB queries, so please make sure to use the cache tag with this: 
{# Build an associative array of user IDs to the count of related entries #}
{% set userData = [] %}

{# Go through all users #}
{% for user in craft.users %}

    {# Get total number of entries this user is the author of #}
    {% set count = craft.entries.authorId(user.id).total() %}

    {# Save entries count and ALL user data we gonna use to our array #}
    {% set userData = userData|merge([{ entriesCount: count, userId: user.id, userName: user.name }]) %}

{% endfor %}

{# Sort, reverse-sort and pick top 10 elements of the array #}
{% set userData = userData|sort|reverse|slice(0, 10) %}

{# Print the top 10 list #}
{% for element in userData %}
    {{ element.userName }} ({{ element.entriesCount }} entries)
{% endfor %}


---

