Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-01T18:51:19.927Z
Answers.LastActivityDate: 2023-11-01T18:51:19.927Z
Answers.UserDisplayName: August Miller
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-28T11:08:57.797Z
Answers.Text: 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.
Answers.UserDisplayName: Piotr Pogorzelski
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-28T18:04:52.780Z
Answers.Text: Wait, I can't make macro using this, macro returns only strings. Oh, well.
Answers.UserDisplayName: Piotr Pogorzelski
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-28T19:38:36.720Z
Answers.Text: 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
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-28T08:43:10.693Z
Answers.LastActivityDate: 2019-02-28T08:43:10.693Z
Answers.UserDisplayName: Robin Schambach
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-31T23:48:20.007Z
Answers.LastActivityDate: 2023-10-31T23:48:20.007Z
Answers.UserDisplayName: Brimby
Body: <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>
  
Tags: <templating>
Title: How do i get sites by group handle/name?
ThreadText: # 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](https://docs.craftcms.com/api/v4/craft-models-site.html) (i.e. the [global `currentSite` Twig variable](https://craftcms.com/docs/4.x/dev/global-variables.html#currentsite)), 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
  
  ```TWIG
  {% 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](https://craftcms.com/docs/4.x/dev/filters.html)
  
  
  ---
  
  