<p>This is a great chance to use the <a href="http://buildwithcraft.com/docs/templating/filters#group">group</a> filter.</p>

<p>First, get all your matrix blocks as an array:</p>

<pre><code>{% set talkBlocks = entry.talks.find() %}
</code></pre>

<p>Then group them by type:</p>

<pre><code>{% set talksByCategories = talkBlocks|group('type') %}
</code></pre>

<p>At this point, you have an associative array where the keys are the block types, and the values are arrays of the blocks with that type. So <code>talksByCategories.workshop</code> or <code>talksByCategories['workshop']</code> will be the workshops.</p>

<p>You can group the workshops by round:</p>

<pre><code>{% set workshopsByRound = talksByCategories.workshop|group('workshopRound.value') %}
</code></pre>

<p>You should check that there were any workshops first (twig's <a href="http://twig.sensiolabs.org/doc/tests/defined.html">defined</a> test):</p>

<pre><code>{% if talksByCategories['workshop'] is defined %} ...
</code></pre>

CC BY-SA 3.0
2015-06-15T20:18:27.260Z
thank you Marion, this looks a lot more succinct than the path I was initially looking at going down with http://craftcms.stackexchange.com/questions/6732/solution-for-matrix-within-matrix-or-more-robust-table-fields mixed with http://craftcms.stackexchange.com/questions/9135/group-by-field-in-matrix-field 

The first part worked but the 2nd I was struggling with. I'll let you know how it goes. Thanks again.
Steven Grant
CC BY-SA 3.0
2015-06-15T20:04:20.703Z
2015-06-15T20:04:20.703Z
Marion Newlevant
<p>I'm wondering if I might have a little too much inception madness in my thinking with this problem.</p>

<p>Enhancing a site we built for a conference last year. As there's an annual conference each year and some of the speakers are the same, we decided to reevaluate the approach.</p>

<p>What we have now is 2 channels, <code>Speaker</code> and <code>Conference</code>. Speaker contains all the speakers that could ever make an appearance at the conference and Conference contains the specific details about that conference.</p>

<p>Within conference, we have a Matrix field called <code>Talks</code> with a <code>workshop</code>, <code>general</code> and <code>special event</code> block.</p>

<p>I want to be able to group talk blocks by their block type, is that possible?</p>

<p>Going a step further, in the case of a <code>workshop</code> - that block has a <code>workshopRound</code> select field of 1, 2 or 3 - I'd love to be able to then group entries with those blocks by their round number.</p>

<p>Any thoughts on how this could be achieved?</p>

<matrix><group><blocks><order>
group Matrix blocks and also order by Matrix field for a given block
# group Matrix blocks and also order by Matrix field for a given block

(Asked by: username_0 on 2015-06-15)

I'm wondering if I might have a little too much inception madness in my thinking with this problem.

Enhancing a site we built for a conference last year. As there's an annual conference each year and some of the speakers are the same, we decided to reevaluate the approach.

What we have now is 2 channels, `Speaker` and `Conference`. Speaker contains all the speakers that could ever make an appearance at the conference and Conference contains the specific details about that conference.

Within conference, we have a Matrix field called `Talks` with a `workshop`, `general` and `special event` block.

I want to be able to group talk blocks by their block type, is that possible?

Going a step further, in the case of a `workshop` - that block has a `workshopRound` select field of 1, 2 or 3 - I'd love to be able to then group entries with those blocks by their round number.

Any thoughts on how this could be achieved?

**Tags:** matrix, group, blocks, order

---

## 1 answers

### Answer by username_1 on 2015-06-15. Score: 6 | Accepted answer

This is a great chance to use the group filter.
First, get all your matrix blocks as an array:
{% set talkBlocks = entry.talks.find() %}

Then group them by type:
{% set talksByCategories = talkBlocks|group('type') %}

At this point, you have an associative array where the keys are the block types, and the values are arrays of the blocks with that type. So talksByCategories.workshop or talksByCategories['workshop'] will be the workshops.
You can group the workshops by round:
{% set workshopsByRound = talksByCategories.workshop|group('workshopRound.value') %}

You should check that there were any workshops first (twig's defined test):
{% if talksByCategories['workshop'] is defined %} ...

> **Comments:**
> **username_0**: thank you Marion, this looks a lot more succinct than the path I was initially looking at going down with http://craftcms.stackexchange.com/questions/6732/solution-for-matrix-within-matrix-or-more-robust-table-fields mixed with http://craftcms.stackexchange.com/questions/9135/group-by-field-in-matrix-field   The first part worked but the 2nd I was struggling with. I'll let you know how it goes. Thanks again.  

---

