<p>You can use the Site component</p>

<h3>PHP</h3>

<pre><code>$groups = Craft::$app-&gt;getSites()-&gt;getAllGroups();
foreach ($groups as $group){
    echo $group-&gt;name;
    foreach ($group-&gt;getSites() as $site){
        echo $site-&gt;name;
    }
}
</code></pre>

<h3>Twig</h3>

<pre><code>{% set groups = craft.app.getSites().getAllGroups() %}
{% for group in groups %}
    {{ group.name }}
    {% for site in group.getSites() %}
        {{ site.name }}
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2018-06-30T07:55:26.687Z
It will return  all sites of all groups
Robin Schambach
CC BY-SA 4.0
2018-06-30T07:56:28.203Z
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
John Stevens
CC BY-SA 4.0
2018-05-23T06:51:39.170Z
2018-06-30T12:34:59.783Z
2018-06-30T12:34:59.783Z
Robin Schambach
<p>We have many country websites on different continents. In the CP we group the sites, where the group has the name of the continent.</p>

<p>Now we want to group the sites in the templates in the same way.</p>

<p>I have this, but it is not giving back the group.</p>

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

{% for group, countriesInGroup in allGroups %}
    {{ group }} &lt;br&gt;
    {% for country in countriesInGroup %}
        {{country.language}} &lt;br&gt;
    {% endfor %}
{% endfor %}
</code></pre>

<p>Hope somebody can point me in the right direction.</p>

<p>Thanks!</p>

<localization><filter><multi-site>
Group Sites by Groups
# Group Sites by Groups

(Asked by: username_0 on 2018-05-22)

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 }} 
    {% for country in countriesInGroup %}
        {{country.language}} 
    {% endfor %}
{% endfor %}

Hope somebody can point me in the right direction.
Thanks!

**Tags:** localization, filter, multi-site


---

## 1 answers

### Answer by username_1 on 2018-05-23. Score: 2 | Accepted answer

You can use the Site component
PHP
$groups = Craft::$app->getSites()->getAllGroups();
foreach ($groups as $group){
    echo $group->name;
    foreach ($group->getSites() as $site){
        echo $site->name;
    }
}

Twig
{% set groups = craft.app.getSites().getAllGroups() %}
{% for group in groups %}
    {{ group.name }}
    {% for site in group.getSites() %}
        {{ site.name }}
    {% endfor %}
{% endfor %}

> **Comments:**
> **username_1**: It will return  all sites of all groups  
> **username_0**: 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  

---

