<p>Something like this should do the trick:</p>

<pre><code>{% set usersGroupedByCountry = craft.users.limit(null)|group('postaddress.country') %}

&lt;ul&gt;
    {% for country, users in usersGroupedByCountry %}
        {% if country and users|length %}
           &lt;li&gt;{{ country }} ({{ users|length }})&lt;/li&gt;
        {% endif %}   
    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>Note that if you want to order the list of countries, you'll need the <a href="https://github.com/TopShelfCraft/SuperSort">SuperSort plugin</a>. Here's how you would sort the list alphabetically:</p>

<pre><code>{% set usersGroupedByCountry = craft.users.limit(null)|group('postaddress.country)|supersort('ksort') %}
</code></pre>

<p>Also note that if you have a lot of users (say, hundreds), it is probably a good idea to wrap the whole thing (including the <code>{% set usersGroupedByCountry ... %}</code> part) in a <a href="https://craftcms.com/docs/templating/cache"><code>{% cache %}</code></a> tag pair, for performance reasons.</p>

CC BY-SA 3.0
2016-10-16T12:48:40.383Z
Thanks Mats, it works perfectly. The only addition I added was the lower filter on postaddress.country because of the different way of writing. (United States, united states, United states, UNITED STATES)
John Stevens
CC BY-SA 3.0
2016-09-29T01:09:30.030Z
2016-09-29T01:09:30.030Z
Mats Mikkel Rummelhoff
<p>We are building a family community website with more than 600 members all over the world.</p>

<p>In <strong>craft.users</strong> we have added many fields and one of them is <strong>country</strong>.</p>

<p>We want to display and count all countries.</p>

<p><strong>Example of the desired output could be something like:</strong></p>

<p>Our members are living in the next 5 countries</p>

<ul>
<li>the Netherlands (545)</li>
<li>Chili (26)</li>
<li>USA (14)</li>
<li>Italy (6)</li>
<li>Sweden (8)</li>
</ul>

CC BY-SA 3.0
2016-09-28T20:00:56.200Z
You may want to provide _much_ more detail than that... We know nothing about your site's architecture. Are countries being stored as Categories, related Entries, or plain text values? Is the country field associated directly with the Users, or via a separate Entry?
Lindsey D
CC BY-SA 3.0
2016-09-28T21:53:29.500Z
Is the `country` field on your Users field layout?  If so, what type of field is it?
Brad Bell
CC BY-SA 3.0
2016-09-28T22:40:51.000Z
For the users address I use Smart Map so the country is like 'postaddress.country'
John Stevens
<templating><filter>
Count all unique values
# Count all unique values

(Asked by: username_0 on 2016-09-28)

We are building a family community website with more than 600 members all over the world.

In **craft.users** we have added many fields and one of them is **country**.

We want to display and count all countries.

**Example of the desired output could be something like:**

Our members are living in the next 5 countries

* the Netherlands (545)
* Chili (26)
* USA (14)
* Italy (6)
* Sweden (8)

**Tags:** templating, filter

> **Comments:**
> **username_1**: You may want to provide _much_ more detail than that... We know nothing about your site's architecture. Are countries being stored as Categories, related Entries, or plain text values? Is the country field associated directly with the Users, or via a separate Entry?  
> **username_2**: Is the `country` field on your Users field layout?  If so, what type of field is it?  
> **username_0**: For the users address I use Smart Map so the country is like 'postaddress.country'  

---

## 1 answers

### Answer by username_3 on 2016-09-29. Score: 5 | Accepted answer

Something like this should do the trick:
{% set usersGroupedByCountry = craft.users.limit(null)|group('postaddress.country') %}


    {% for country, users in usersGroupedByCountry %}
        {% if country and users|length %}
           {{ country }} ({{ users|length }})
        {% endif %}   
    {% endfor %}


Note that if you want to order the list of countries, you'll need the SuperSort plugin. Here's how you would sort the list alphabetically:
{% set usersGroupedByCountry = craft.users.limit(null)|group('postaddress.country)|supersort('ksort') %}

Also note that if you have a lot of users (say, hundreds), it is probably a good idea to wrap the whole thing (including the {% set usersGroupedByCountry ... %} part) in a {% cache %} tag pair, for performance reasons.

> **Comments:**
> **username_0**: Thanks Mats, it works perfectly. The only addition I added was the lower filter on postaddress.country because of the different way of writing. (United States, united states, United states, UNITED STATES)  

---

