Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-28T21:32:01.480Z
Answers.LastActivityDate: 2019-08-28T21:32:01.480Z
Answers.UserDisplayName: Jon Smith
Body: <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>
  
Tags: <matrix><categories><relatedto>
Title: relatedTo a category in a matrix field
ThreadText: # 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() %}
  
          <section id="{{ category.title }}">
              <h1>{{ category.title }}</h1>
              {% for block in entry.teamMember.all() %}
              <section class="member">
                  {% set image = block.photograph.one() %}
                  {% if image %}
                  <div style="background-image: url('{{ image.getUrl('image500') }}');">&nbsp;</div>
                  {% endif %}
  
                  <h1>{{ block.fullName }} <em>{{ block.jobTitle|markdown }}</em></h1>
                  {{ block.biography }}
              </section>
              {% endfor %}
  
          </section>
          {% 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() %}
  
      <section id="{{ category.title }}">
          {% if category.title != 'Ownership' %}<h1>{{ category.title }}</h1>{% endif %}
          {% for block in entry.teamMember.relatedTo(category).all() %}
          <section class="member">
              {% set image = block.photograph.one() %}
              {% if image %}
              <div style="background-image: url('{{ image.getUrl('image500') }}');">&nbsp;</div>
              {% endif %}
  
              <h1>{{ block.fullName }} <em>{{ block.jobTitle|markdown }}</em></h1>
              {{ block.biography }}
          </section>
          {% endfor %}
  
      </section>
      {% endfor %}
  
  ```
  
  
  ---
  
  