1

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 }} <br>

  {% 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...
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".

  • That doesn't work. Because the deals have the category and the field relating to the country. – CreateSean Apr 16 '18 at 15:09
1

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 #}
  <h4>{{ category.title }}</h4>

  <ul>
    {# inside categories list countries #}
    {% for country in countries %}
      <li>
      {{ country.title }}
        <ul>
          {# 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 %}
              <li> {{ entry.title }} </li>
            {% endfor %}
        </ul>
      </li>
    {% endfor %}
  </ul>
{% endfor %}{# end categories #}

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.