We have many country websites on different continents. In the CP we group the sites, where the group has the name of the continent.

Now we want to group the sites in the templates in the same way.

I have this, but it is not giving back the group.

{% set allSites = craft.app.sites.getAllSites() %}
{% set allGroups = allSites|group('allSites.group') %}

{% for group, countriesInGroup in allGroups %}
    {{ group }} <br>
    {% for country in countriesInGroup %}
        {{country.language}} <br>
    {% endfor %}
{% endfor %}

Hope somebody can point me in the right direction.


1 Answer 1


You can use the Site component


$groups = Craft::$app->getSites()->getAllGroups();
foreach ($groups as $group){
    echo $group->name;
    foreach ($group->getSites() as $site){
        echo $site->name;


{% set groups = craft.app.getSites().getAllGroups() %}
{% for group in groups %}
    {{ group.name }}
    {% for site in group.getSites() %}
        {{ site.name }}
    {% endfor %}
{% endfor %}
  • It will return all sites of all groups Jun 30, 2018 at 7:55
  • Ok, picking up this task again. Unfortunately, it's not returning the site that belongs to the groups. This is my result on your twig code example Robin: Africa Asia Australia Europe North America South America Jun 30, 2018 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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