I have a Structure called "Stoves". Each Entry has two Category fields 'fuelType' and 'output'.

  • Fuel Type categories = ''Wood Burning','Multi-Fuel','Gas' and 'Electric'.
  • Output categories = '4kW','6kW','8kW','10kW',

On the category page template the following simple code gives me the correct entries based on the 'fuelType' field... e.g. when viewing the 'Wood burning' page it shows me Wood Burning stoves.

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

{% for entry in entries %}
    <a href="{{ entry.url }}">{{ entry.title }}</a><br>
{% endfor %}

But I now need to also loop through the 'output' category group so I get something like the following:

ALL Wood Burning Stoves

Intro Text

start loop

  • 4kW Stoves

  • Stove_4kW_No.1

  • Stove_4kW_No.2

  • Stove_4kW_No.3

  • 6kW Stoves

  • Stove_6kW_No.1

  • Stove_6kW_No.2

  • 8kW Stoves

  • Stove_8kW_No.1

  • Stove_8kW_No.2

  • Stove_8kW_No.3

  • Stove_8kW_No.4

...and so on....

end loop

Is this even possible? Alternatively could I pull with individual entry queries using the individual 'output' category slugs?

Many thanks

1 Answer 1


You'd typically use the group filter for this. Depending on what you need to output, be sure to also use eager loading to improve performance.

{% set groupedEntries = craft.entries.with(['yourOutputCatFieldHandle']).relatedTo(category).all()|group('yourOutputCatFieldHandle[0].title') %}

{% for cat, entries in groupedEntries %}
    <h3>{{ cat }}</h3>
        {% for entry in entries %}
            <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
        {% endfor %}
{% endfor %}

EDIT: alternative answer below to allow for using the correct category ordering as per the control panel's Structure order.

Note: this approach requires the supersort plugin.

{# ======================================
First, fetch entries related to this category,
then group the array by each category's
`lft` structure position, then sort the array
by those keys with supersort's ksort function
========================================= #}

{% set groupedEntries = craft.entries.with([

{# ======================================
Next, create a hash map of those categories so we can
match up the `lft` left structure position
with the category's title later.
(google "Nested Sets" if you're really bored)
========================================= #}

{% if groupedEntries|length %}
    {% set catTitlesMap = craft.categories.group('yourOutputCatGROUPHandle').all()|group('lft') %}
{% endif %}

{# ======================================
Finally, loop through the grouped array,
matching up the accessory's `lft` position
with the hash map to get the right title...
========================================= #}

{% for cat, entries in groupedEntries %}
    <h3>{{ catTitlesMap[cat][0].title }}</h3>
        {% for entry in entries %}
            <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
        {% endfor %}
{% endfor %}
  • Hi James, thanks for looking at this. Your code is almost working. Initially I get an error "Impossible to access an attribute ("title") on a string variable ("5kW")" where '5kW' is the title of one of the categories and probably the first the loop encounters. If I remove {{ cat.title }} it does indeed output the entries grouped by their category - but without the Category title ('cos I removed it). Also, it seems to output the categories in date order when I need them in the actual order they appear in the CP. 4kW, 6kW, 8kW ..etc. So, how to get the {{ cat.title }} and orderBy working?
    – Martin
    Aug 20, 2020 at 9:45
  • Sorry that was a typo, corrected now... it should just be {{ cat }} as it's just a simple string (we already called .title in the grouping filter). For outputting in the CP-based order of the categories, that's a bit more complicated as our initial call is for the entries, not the categories... I'll have a think about it. Maybe something is possible with custom parameters: craftcms.com/docs/3.x/dev/… Aug 20, 2020 at 10:19
  • Brilliant, thanks James that worked! Now to get the categories output in the right order :)
    – Martin
    Aug 20, 2020 at 10:38
  • I've updated the answer with a potential alternative approach to get the order correct. Aug 20, 2020 at 10:47
  • Sorted! (literally). This is great James thank you so much for your time.
    – Martin
    Aug 20, 2020 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.