TransWikia.com

Display related entries on a category page filtered by another category field

Craft CMS Asked on January 9, 2021

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

One Answer

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>
    <ul>
        {% for entry in entries %}
            <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
        {% endfor %}
    </ul>
{% 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([
        'yourOutputCatFieldHandle'
    ])
    .relatedTo(category)
    .all()|group('yourOutputCatFieldHandle[0].lft')|supersort('ksort')
%}

{# ======================================
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>
    <ul>
        {% for entry in entries %}
            <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
        {% endfor %}
    </ul>
{% endfor %}

Correct answer by James Smith on January 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP