<p>If you already have a reference to a <a href="https://docs.craftcms.com/api/v4/craft-models-site.html" rel="nofollow noreferrer"><code>Site</code> model</a> (i.e. the <a href="https://craftcms.com/docs/4.x/dev/global-variables.html#currentsite" rel="nofollow noreferrer">global <code>currentSite</code> Twig variable</a>), you can get other sites in that group, like this:</p>
<pre><code>{% set currentSiteGroup = currentSite.group %}
{% set sitesInGroup = currentSiteGroup.sites %}

{# Bonus — Remove the current site from that list: #}
{% set otherSitesInGroup = sitesInGroup|without(currentSite) %}
</code></pre>

CC BY-SA 4.0
2023-11-01T18:51:19.927Z
2023-11-01T18:51:19.927Z
August Miller
<p>I would rather suggest you to do that in PHP but the Twig way would be the following</p>

<pre class="lang-TWIG prettyprint-override"><code>{% set allSites = craft.app.getSites().getAllSites() %}
{% set sitesWithGroup = [] %}
{% for site in allSites %}
    {% if site.getGroup().name == 'my group name' %}
        {% set sitesWithGroup = sitesWithGroup|merge([site]) %}
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2019-02-28T11:08:57.797Z
Thanks! I think i will turn it into macro. And about php, i prefear to do things in twig when its possible, its just less posibility to break something.
Piotr Pogorzelski
CC BY-SA 4.0
2019-02-28T18:04:52.780Z
Wait, I can't make macro using this, macro returns only strings. Oh, well.
Piotr Pogorzelski
CC BY-SA 4.0
2019-02-28T19:38:36.720Z
That's why you usually want to do it in PHP since it's less likely to break and you have more possiblies + it's less resource heavy
Robin Schambach
CC BY-SA 4.0
2019-02-28T08:43:10.693Z
2019-02-28T08:43:10.693Z
Robin Schambach
<p>I find Twig/Craft filters to be the most concise and readable for things like this.</p>
<pre><code>{% set allSites = craft.app.sites.getAllSites() %}
{% set filteredSites = allSites|filter(s =&gt; s.group == 'My Group Name') %}
</code></pre>
<p>More useful filters <a href="https://craftcms.com/docs/4.x/dev/filters.html" rel="nofollow noreferrer">here</a></p>

CC BY-SA 4.0
2023-10-31T23:48:20.007Z
2023-10-31T23:48:20.007Z
Brimby
<p>How can I get sites by their group handle/name within twig templates?
Documentation only mentions querying sites by group id - <code>getSitesByGroupId</code>.</p>

<templating>
How do i get sites by group handle/name?
# How do i get sites by group handle/name?

(Asked by: username_0 on 2019-02-28)

How can I get sites by their group handle/name within twig templates?
Documentation only mentions querying sites by group id - `getSitesByGroupId`.

**Tags:** templating


---

## 3 answers

### Answer by username_1 on 2023-11-01. Score: 1 | Accepted answer

If you already have a reference to a Site model (i.e. the global currentSite Twig variable), you can get other sites in that group, like this:
{% set currentSiteGroup = currentSite.group %}
{% set sitesInGroup = currentSiteGroup.sites %}

{# Bonus — Remove the current site from that list: #}
{% set otherSitesInGroup = sitesInGroup|without(currentSite) %}


---

### Answer by username_2 on 2019-02-28. Score: 2

I would rather suggest you to do that in PHP but the Twig way would be the following
{% set allSites = craft.app.getSites().getAllSites() %}
{% set sitesWithGroup = [] %}
{% for site in allSites %}
    {% if site.getGroup().name == 'my group name' %}
        {% set sitesWithGroup = sitesWithGroup|merge([site]) %}
    {% endif %}
{% endfor %}

> **Comments:**
> **username_0**: Thanks! I think i will turn it into macro. And about php, i prefear to do things in twig when its possible, its just less posibility to break something.  
> **username_0**: Wait, I can't make macro using this, macro returns only strings. Oh, well.  
> **username_2**: That's why you usually want to do it in PHP since it's less likely to break and you have more possiblies + it's less resource heavy  

---

### Answer by username_3 on 2023-10-31. Score: 1

I find Twig/Craft filters to be the most concise and readable for things like this.
{% set allSites = craft.app.sites.getAllSites() %}
{% set filteredSites = allSites|filter(s => s.group == 'My Group Name') %}

More useful filters here


---

