Answers.Body: <p>I may not understand fully, but it sounds like you could just loop over the attached Department Categories, then query for Staff related to that category:</p>
  
  <pre><code>{# Load the Departments Categories: #}
  {% set departments = entry.allDepartments.all() %}
  
  {% for department in departments %}
    {# Find all the Staff Members associated with the department: #}
    {% set staff = craft.entries({
      section: 'staffMembers',
      relatedTo: department,
      with: ['staffPhoto']
    }).all() %}
  
    &lt;h1&gt;{{ department.title }}&lt;/h1&gt;
    &lt;ul&gt;
      {# Loop over the Staff Members: #}
      {% for person in staff %}
        &lt;li&gt;{{ person.title }}&lt;/li&gt;
      {% endfor %}
    &lt;/ul&gt;
  {% endfor %}
  </code></pre>
  
  <blockquote>
    <p>There is a (very slight) performance penalty for <a href="https://stackoverflow.com/questions/97197/what-is-the-n1-selects-problem-in-orm-object-relational-mapping">performing queries like this in a loop</a>, but it sounds like each of these focused sub-pages won't have <em>too</em> many Departments listed anyway.</p>
  </blockquote>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-13T09:44:58.253Z
Answers.Text: Perfect, that worked nicely. Thanks so much!
Answers.UserDisplayName: Mark
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-13T09:16:54.027Z
Answers.LastActivityDate: 2019-12-13T09:16:54.027Z
Answers.UserDisplayName: August Miller
Body: <p><strong>Background</strong>:</p>
  
  <p>I have a listing overview page that lists teaching staff grouped by their department (A category group holds all departments). E.g.</p>
  
  <pre><code>All Staff Members:
  
  -- Science --
      Mrs Magnesium
      Mr Bunson
  
   -- P.E --
      Mr Stretch
      Mrs Bend
  
  -- Maths -- 
      Ms Multiplication
      Mr Trig
  </code></pre>
  
  <p>To achieve the above I am using this code, which is working perfectly on a single page:</p>
  
  <pre><code>{% set entries = craft.entries.section('staffMembers').with(['staffPhoto']).all() %}
  {% for category, catEntries in entries | group('staffGroup.one().title') %}
      &lt;div&gt;
      {% for entry in catEntries %}                
          &lt;h3&gt;{{ entry.staffTitle }}&lt;/h3&gt;  
          &lt;p&gt;{{ entry.staffBio }}&lt;/p&gt;     
      {% endfor %}
      &lt;/div&gt;
  {% endfor %}
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <p>The client now needs to split these staff members across different pages, like the example below. So in order to achieve this I want them to be able use a category field (<code>entry.staffGroups</code>) where they can select multiple categories (departments) to display on each page. <em>Hope I am making sense!</em></p>
  
  <p>Science Staff Page:</p>
  
  <pre><code>-- Science --
      Mrs Magnesium
      Mr Bunson
  </code></pre>
  
  <p>Physical Education Staff Page:</p>
  
  <pre><code> -- P.E --
      Mr Stretch
      Mrs Bend
  </code></pre>
  
  <p>Mathematics Staff Page</p>
  
  <pre><code>-- Maths -- 
      Ms Multiplication
      Mr Trig
  </code></pre>
  
  <p>I am struggling to achieve this, my attempt is below:</p>
  
  <pre><code>{# get the selected categories (departments) from category field #}
  {% set departments = entry.allDepartments.all() %}
  
  {# Set the entries, and attempt to relate them to ONLY the selected departments.#}
  {% set entries = craft.entries.section('staffMembers').relatedTo(departments|join(',')).with(['staffPhoto']).all() %}
      {% for category, catEntries in entries | group('staffGroup.one().title') %}
          &lt;div&gt;
          {% for entry in catEntries %}                
              &lt;h3&gt;{{ entry.staffTitle }}&lt;/h3&gt;  
              &lt;p&gt;{{ entry.staffBio }}&lt;/p&gt;     
          {% endfor %}
          &lt;/div&gt;
      {% endfor %}
  </code></pre>
  
  <p>Example of entry:</p>
  
  <p><a href="https://i.sstatic.net/NmRUY.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/NmRUY.png" alt="Example of entry"></a></p>
  
  <p>Thanks!!</p>
  
Tags: <categories><query>
Title: Get entries with related to selected categories and then group by category
ThreadText: # Get entries with related to selected categories and then group by category
  
  (Asked by: username_0 on 2019-12-12)
  
  **Background**:
  
  I have a listing overview page that lists teaching staff grouped by their department (A category group holds all departments). E.g.
  
  ```
  All Staff Members:
  
  -- Science --
      Mrs Magnesium
      Mr Bunson
  
   -- P.E --
      Mr Stretch
      Mrs Bend
  
  -- Maths -- 
      Ms Multiplication
      Mr Trig
  
  ```
  
  To achieve the above I am using this code, which is working perfectly on a single page:
  
  ```
  {% set entries = craft.entries.section('staffMembers').with(['staffPhoto']).all() %}
  {% for category, catEntries in entries | group('staffGroup.one().title') %}
      <div>
      {% for entry in catEntries %}                
          <h3>{{ entry.staffTitle }}</h3>  
          <p>{{ entry.staffBio }}</p>     
      {% endfor %}
      </div>
  {% endfor %}
  
  ```
  
  **Question:**
  
  The client now needs to split these staff members across different pages, like the example below. So in order to achieve this I want them to be able use a category field (`entry.staffGroups`) where they can select multiple categories (departments) to display on each page. *Hope I am making sense!*
  
  Science Staff Page:
  
  ```
  -- Science --
      Mrs Magnesium
      Mr Bunson
  
  ```
  
  Physical Education Staff Page:
  
  ```
   -- P.E --
      Mr Stretch
      Mrs Bend
  
  ```
  
  Mathematics Staff Page
  
  ```
  -- Maths -- 
      Ms Multiplication
      Mr Trig
  
  ```
  
  I am struggling to achieve this, my attempt is below:
  
  ```
  {# get the selected categories (departments) from category field #}
  {% set departments = entry.allDepartments.all() %}
  
  {# Set the entries, and attempt to relate them to ONLY the selected departments.#}
  {% set entries = craft.entries.section('staffMembers').relatedTo(departments|join(',')).with(['staffPhoto']).all() %}
      {% for category, catEntries in entries | group('staffGroup.one().title') %}
          <div>
          {% for entry in catEntries %}                
              <h3>{{ entry.staffTitle }}</h3>  
              <p>{{ entry.staffBio }}</p>     
          {% endfor %}
          </div>
      {% endfor %}
  
  ```
  
  Example of entry:
  
  [![Example of entry](https://i.sstatic.net/NmRUY.png)](https://i.sstatic.net/NmRUY.png)
  
  Thanks!!
  
  **Tags:** categories, query
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-12-13. Score: 1 | Accepted answer
  
  I may not understand fully, but it sounds like you could just loop over the attached Department Categories, then query for Staff related to that category:
  
  ```
  {# Load the Departments Categories: #}
  {% set departments = entry.allDepartments.all() %}
  
  {% for department in departments %}
    {# Find all the Staff Members associated with the department: #}
    {% set staff = craft.entries({
      section: 'staffMembers',
      relatedTo: department,
      with: ['staffPhoto']
    }).all() %}
  
    <h1>{{ department.title }}</h1>
    <ul>
      {# Loop over the Staff Members: #}
      {% for person in staff %}
        <li>{{ person.title }}</li>
      {% endfor %}
    </ul>
  {% endfor %}
  
  ```
  
  > There is a (very slight) performance penalty for [performing queries like this in a loop](https://stackoverflow.com/questions/97197/what-is-the-n1-selects-problem-in-orm-object-relational-mapping), but it sounds like each of these focused sub-pages won't have *too* many Departments listed anyway.
  
  > **Comments:**
  > **username_0**: Perfect, that worked nicely. Thanks so much!  
  
  ---
  
  