<p>Figured it out:</p>

<pre><code>    {# Fetch entries with a Matrix block to get non empty categories #}
    {% for block in entry.teamMember.all() %}
    {% set category = block.department.one() %}

    &lt;section id="{{ category.title }}"&gt;
        {% if category.title != 'Ownership' %}&lt;h1&gt;{{ category.title }}&lt;/h1&gt;{% endif %}
        {% for block in entry.teamMember.relatedTo(category).all() %}
        &lt;section class="member"&gt;
            {% set image = block.photograph.one() %}
            {% if image %}
            &lt;div style="background-image: url('{{ image.getUrl('image500') }}');"&gt;&amp;nbsp;&lt;/div&gt;
            {% endif %}

            &lt;h1&gt;{{ block.fullName }} &lt;em&gt;{{ block.jobTitle|markdown }}&lt;/em&gt;&lt;/h1&gt;
            {{ block.biography }}
        &lt;/section&gt;
        {% endfor %}

    &lt;/section&gt;
    {% endfor %}
</code></pre>

CC BY-SA 4.0
2019-08-28T21:32:01.480Z
2019-08-28T21:32:01.480Z
Jon Smith
<p>All, I'm very close, but missing something and any help is appreciated. I have a teamMember matrix block allowing me to add employees with some basic info. One of the fields is a category field called 'department'. I"m trying to list the departments, and within them, the employee information.</p>

<p>Currently my output is this: </p>

<pre><code>Marketing
  marketing member one
  marketing member two
  creative member one
  creative member two

Creative
  marketing member one
  marketing member two
  creative member one
  creative member two
</code></pre>

<p>However, I'm looking to achieve this:</p>

<pre><code>Marketing 
  marketing member one
  marketing member two

Creative
 creative member one
 creative member two
</code></pre>

<p>It seems that I'm doing something wrong with <code>relatedTo</code>.  </p>

<p>My code: </p>

<pre><code>        {# Fetch entries with a Matrix block to get non empty categories #}
        {% for block in entry.teamMember.all() %}
        {% set category = block.department.one() %}

        {# Fetch entries with a Matrix block related to the category #}
        {% set entries = craft.entries.relatedTo(category).all() %}

        &lt;section id="{{ category.title }}"&gt;
            &lt;h1&gt;{{ category.title }}&lt;/h1&gt;
            {% for block in entry.teamMember.all() %}
            &lt;section class="member"&gt;
                {% set image = block.photograph.one() %}
                {% if image %}
                &lt;div style="background-image: url('{{ image.getUrl('image500') }}');"&gt;&amp;nbsp;&lt;/div&gt;
                {% endif %}

                &lt;h1&gt;{{ block.fullName }} &lt;em&gt;{{ block.jobTitle|markdown }}&lt;/em&gt;&lt;/h1&gt;
                {{ block.biography }}
            &lt;/section&gt;
            {% endfor %}

        &lt;/section&gt;
        {% endfor %}
</code></pre>

<p>What am I missing? Thank you in advance!</p>

<matrix><categories><relatedto>
relatedTo a category in a matrix field
# relatedTo a category in a matrix field

(Asked by: username_0 on 2019-08-28)

All, I'm very close, but missing something and any help is appreciated. I have a teamMember matrix block allowing me to add employees with some basic info. One of the fields is a category field called 'department'. I"m trying to list the departments, and within them, the employee information.
Currently my output is this: 
Marketing
  marketing member one
  marketing member two
  creative member one
  creative member two

Creative
  marketing member one
  marketing member two
  creative member one
  creative member two

However, I'm looking to achieve this:
Marketing 
  marketing member one
  marketing member two

Creative
 creative member one
 creative member two

It seems that I'm doing something wrong with relatedTo.  
My code: 
        {# Fetch entries with a Matrix block to get non empty categories #}
        {% for block in entry.teamMember.all() %}
        {% set category = block.department.one() %}

        {# Fetch entries with a Matrix block related to the category #}
        {% set entries = craft.entries.relatedTo(category).all() %}

        
            {{ category.title }}
            {% for block in entry.teamMember.all() %}
            
                {% set image = block.photograph.one() %}
                {% if image %}
                 
                {% endif %}

                {{ block.fullName }} {{ block.jobTitle|markdown }}
                {{ block.biography }}
            
            {% endfor %}

        
        {% endfor %}

What am I missing? Thank you in advance!

**Tags:** matrix, categories, relatedto

---

## 1 answers

### Answer by username_0 on 2019-08-28. Score: 1

Figured it out:
    {# Fetch entries with a Matrix block to get non empty categories #}
    {% for block in entry.teamMember.all() %}
    {% set category = block.department.one() %}

    
        {% if category.title != 'Ownership' %}{{ category.title }}{% endif %}
        {% for block in entry.teamMember.relatedTo(category).all() %}
        
            {% set image = block.photograph.one() %}
            {% if image %}
             
            {% endif %}

            {{ block.fullName }} {{ block.jobTitle|markdown }}
            {{ block.biography }}
        
        {% endfor %}

    
    {% endfor %}


---

