<p>I figured this out in part with @syversen answer, but it need some tweaking</p>

<p>Here's the simplified working code. This </p>

<pre><code>{% set countries = craft.entries.section('countries') %}
{% set publications = craft.entries.section('publicationsChannel') %}
{% set categories = craft.categories.group('documentCategory') %}

{% for category in categories %}
{# list categories #}
  &lt;h4&gt;{{ category.title }}&lt;/h4&gt;

  &lt;ul&gt;
    {# inside categories list countries #}
    {% for country in countries %}
      &lt;li&gt;
      {{ country.title }}
        &lt;ul&gt;
          {# finally output entries that are in the category and country #}
          {% set relatedEntries = publications.relatedTo([
          'and',
          {targetElement: category},
          {targetElement: country, field: 'regionCountry'}
          ]) %}

            {% for entry in relatedEntries %}
              &lt;li&gt; {{ entry.title }} &lt;/li&gt;
            {% endfor %}
        &lt;/ul&gt;
      &lt;/li&gt;
    {% endfor %}
  &lt;/ul&gt;
{% endfor %}{# end categories #}
</code></pre>

CC BY-SA 3.0
2018-04-16T15:35:50.590Z
2018-04-16T15:35:50.590Z
CreateSean
<p>If you do like this:</p>

<p>The entries "countries" have two fields for relations, category and entries.</p>

<p>Then for every category in CategoryGroupA you can loop over the entries related to it (countries) and for every country loop over the related entries.</p>

<pre><code>{% set categories = craft.categories.group('CategoryGroupA') %}

{% for category in categories %}

    //Print Category here (Catregory 1, 2 etc.)

    {% set countries = craft.entries.relatedTo(category) %}

    {% for country in countries %}

         //Print entries here (Canada, US, UK etc.)

        {% set entries= craft.entries.relatedTo(country) %}

        {% for entry in entries %}
            //And here you get entry 1, 2, 3 etc..
        {% endfor %}

    {% endfor %}

{% endfor %}
</code></pre>

<p>For example the entry "Canada", in the category field it would be related to "Category 1" and "Category 2". And in the Entries field it would be related to "Entry 1" and "Entry 2".</p>

CC BY-SA 3.0
2018-04-16T15:09:08.783Z
That doesn't work. Because the deals have the category and the field relating to the country.
CreateSean
CC BY-SA 3.0
2018-04-13T22:26:27.693Z
2018-04-13T22:26:27.693Z
Syversen
<p>I have a channel with categories and an entries field. I need to first group all entries by their category. This is working fine.</p>

<pre><code>{% set entries = craft.entries.section('publicationsChannel') %}
{% set relatedCats = craft.categories.relatedTo(entries) %}

{% for category in relatedCats %}
   {{ category.title }}

  {% for entry in relatedEntries %}
    {# this list of entries needs to be grouped by the regionCountry field #}
    {{ entry.title }} &lt;br&gt;

  {% endfor %}
{% endfor %}
</code></pre>

<p>However what I now need to do is take the list of entries and further organize them according to a relationship field handle regionCountry</p>

<p>There are only 3 entries in that channel Canada, US, UK and they need to be grouped in that order.</p>

<p>I can't seem to wrap my head around the logic needed to do this.</p>

<p>What I need finally is.</p>

<ul>
<li><p>Category 1</p>

<ul>
<li><ul>
<li>Canada</li>
</ul></li>
<li><ul>
<li><ul>
<li>entry 1</li>
</ul></li>
</ul></li>
<li><ul>
<li><ul>
<li>entry 2</li>
</ul></li>
</ul></li>
<li><ul>
<li>US</li>
</ul></li>
<li><ul>
<li><ul>
<li>entry 1</li>
</ul></li>
</ul></li>
<li><ul>
<li><ul>
<li>entry 2</li>
</ul></li>
</ul></li>
<li><ul>
<li>UK</li>
</ul></li>
<li><ul>
<li><ul>
<li>entry 1</li>
</ul></li>
</ul></li>
<li><ul>
<li><ul>
<li>entry 2</li>
</ul></li>
</ul></li>
</ul></li>
<li><p>Category 2</p>

<ul>
<li><ul>
<li>Canada</li>
</ul></li>
<li><ul>
<li><ul>
<li>entry 1</li>
</ul></li>
</ul></li>
<li><ul>
<li><ul>
<li>entry 2</li>
</ul></li>
</ul></li>
<li><ul>
<li>US</li>
</ul></li>
<li><ul>
<li><ul>
<li>entry 1</li>
</ul></li>
</ul></li>
<li><ul>
<li><ul>
<li>entry 2</li>
</ul></li>
</ul></li>
<li><ul>
<li>UK</li>
</ul></li>
<li><ul>
<li><ul>
<li>entry 1</li>
</ul></li>
</ul></li>
<li><ul>
<li><ul>
<li>entry 2
etc...</li>
</ul></li>
</ul></li>
</ul></li>
</ul>

<templating><categories><craft3><entries-field><relatedto>
Group entries by Category and then in each category by related entry field
# Group entries by Category and then in each category by related entry field

(Asked by: username_0 on 2018-04-13)

I have a channel with categories and an entries field. I need to first group all entries by their category. This is working fine.
{% set entries = craft.entries.section('publicationsChannel') %}
{% set relatedCats = craft.categories.relatedTo(entries) %}

{% for category in relatedCats %}
   {{ category.title }}

  {% for entry in relatedEntries %}
    {# this list of entries needs to be grouped by the regionCountry field #}
    {{ entry.title }} 

  {% endfor %}
{% endfor %}

However what I now need to do is take the list of entries and further organize them according to a relationship field handle regionCountry
There are only 3 entries in that channel Canada, US, UK and they need to be grouped in that order.
I can't seem to wrap my head around the logic needed to do this.
What I need finally is.

Category 1


Canada



entry 1




entry 2



US



entry 1




entry 2



UK



entry 1




entry 2



Category 2


Canada



entry 1




entry 2



US



entry 1




entry 2



UK



entry 1




entry 2
etc...

**Tags:** templating, categories, craft3, entries-field, relatedto


---

## 2 answers

### Answer by username_0 on 2018-04-16. Score: 2

I figured this out in part with @syversen answer, but it need some tweaking
Here's the simplified working code. This 
{% set countries = craft.entries.section('countries') %}
{% set publications = craft.entries.section('publicationsChannel') %}
{% set categories = craft.categories.group('documentCategory') %}

{% for category in categories %}
{# list categories #}
  {{ category.title }}

  
    {# inside categories list countries #}
    {% for country in countries %}
      
      {{ country.title }}
        
          {# finally output entries that are in the category and country #}
          {% set relatedEntries = publications.relatedTo([
          'and',
          {targetElement: category},
          {targetElement: country, field: 'regionCountry'}
          ]) %}

            {% for entry in relatedEntries %}
               {{ entry.title }} 
            {% endfor %}
        
      
    {% endfor %}
  
{% endfor %}{# end categories #}


---

### Answer by username_1 on 2018-04-13. Score: 1

If you do like this:
The entries "countries" have two fields for relations, category and entries.
Then for every category in CategoryGroupA you can loop over the entries related to it (countries) and for every country loop over the related entries.
{% set categories = craft.categories.group('CategoryGroupA') %}

{% for category in categories %}

    //Print Category here (Catregory 1, 2 etc.)

    {% set countries = craft.entries.relatedTo(category) %}

    {% for country in countries %}

         //Print entries here (Canada, US, UK etc.)

        {% set entries= craft.entries.relatedTo(country) %}

        {% for entry in entries %}
            //And here you get entry 1, 2, 3 etc..
        {% endfor %}

    {% endfor %}

{% endfor %}

For example the entry "Canada", in the category field it would be related to "Category 1" and "Category 2". And in the Entries field it would be related to "Entry 1" and "Entry 2".

> **Comments:**
> **username_0**: That doesn't work. Because the deals have the category and the field relating to the country.  

---

