<p>Just answering this for anyone that may come across this in the future as I have...</p>

<p>Could you reuse the group tag?</p>

<pre><code>{% for block in entry.myMatrixField.group(&lt;catgegory here&gt;) %}
    ...
{% endfor %}
</code></pre>

<p>You have probably resolved it due to the age, but your answer would be helpful.</p>

CC BY-SA 4.0
2018-08-20T13:53:57.210Z
2018-08-20T13:53:57.210Z
Jay
<p>I have a matrix field with a category field. I need to group these matrix items by category and give them headings which I'll use for tabs.</p>

<p>This is how I'd do it for entries but is it possible for matrix?</p>

<pre><code>&lt;ul class="tabs"&gt;
    {% nav category in craft.categories.group('otherAppliances') %}

    {% set itemCount = craft.entries.relatedTo(category).total() %}
        {% if itemCount %}

            &lt;li rel=""&gt;{{ category.title }}&lt;/li&gt;

        {% endif %}
    {% endnav %}
&lt;/ul&gt;
</code></pre>

<p>I think my question may be similar to <a href="https://craftcms.stackexchange.com/questions/2524/how-to-query-with-relatedto-of-a-relationship-field-in-a-matrix-field-block">How to query with relatedTo of a Relationship Field in a Matrix Field block</a> but I can't get my head around it.</p>

<p><strong>Matrix setup:</strong></p>

<p><a href="https://i.sstatic.net/iNUGO.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/iNUGO.png" alt="enter image description here"></a></p>

<matrix><categories><relations><relatedto>
Categories within matrix for relatedTo
# Categories within matrix for relatedTo

(Asked by: username_0 on 2017-03-28)

I have a matrix field with a category field. I need to group these matrix items by category and give them headings which I'll use for tabs.
This is how I'd do it for entries but is it possible for matrix?

    {% nav category in craft.categories.group('otherAppliances') %}

    {% set itemCount = craft.entries.relatedTo(category).total() %}
        {% if itemCount %}

            {{ category.title }}

        {% endif %}
    {% endnav %}


I think my question may be similar to How to query with relatedTo of a Relationship Field in a Matrix Field block but I can't get my head around it.
Matrix setup:

**Tags:** matrix, categories, relations, relatedto

---

## 1 answers

### Answer by username_1 on 2018-08-20. Score: 1

Just answering this for anyone that may come across this in the future as I have...
Could you reuse the group tag?
{% for block in entry.myMatrixField.group() %}
    ...
{% endfor %}

You have probably resolved it due to the age, but your answer would be helpful.


---

