This is the setup:

I have a Matrix field called Breads. There is 1 block called Bread.

Within, I have two fields:

  • An Entries field called Selected bread (limited to 1 entry)
  • A Number field called Quantity.

Lets say I have 3 rows. Row 1 and 3 have the same entry selected, but different quantities. Is there a way to group by the Bread entry id, but get the different quantities?

The goal is to consider all entries with same id as "one", and to make it easy to sum them together.

1 Answer 1


Try something like:

{% set breadGroups = entry.breads | group('bread.first.id') %}

{% for breadId, blocks in breadGroups %}

    {% set sum = 0 %}

    {% for block in blocks %}

        {% set sum = sum + block.quantity %}

    {% endfor %}

    Total qty for Bread ID {{ breadId }}: {{ sum }}

 {% endfor %}

The group filter consumes a list — in this base, an array of Matrix blocks — and produces an array of arrays where the key is the grouping parameter, and the value is an array of items from the original list for which the group criteria matches the key.

c.f. Craft docs: http://buildwithcraft.com/docs/templating/filters#group


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.