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


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 %}
  • 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.
    – piotrpog
    Feb 28 '19 at 11:08
  • Wait, I can't make macro using this, macro returns only strings. Oh, well.
    – piotrpog
    Feb 28 '19 at 18:04
  • 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 Feb 28 '19 at 19:38

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.