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)
  • 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?
    – Lindsey D
    Sep 28, 2016 at 20:00
  • 1
    Is the country field on your Users field layout? If so, what type of field is it?
    – Brad Bell
    Sep 28, 2016 at 21:53
  • 1
    For the users address I use Smart Map so the country is like 'postaddress.country' Sep 28, 2016 at 22:40

1 Answer 1


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 %}
           <li>{{ country }} ({{ users|length }})</li>
        {% 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.

  • 1
    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) Oct 16, 2016 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.