Answers.Body: <p>Start by looping thru each category. Say, all categories in a category group called "eventsCategories." Then find entries related to each category (if any).</p>
  
  <pre class="lang-twig prettyprint-override"><code>{# loop through each of the categories #}
  {% for category in craft.categories.group('eventsCategories') %}
  
    {# find the exhibitor entries that are related to both the Event entry and category #}
    {% set categoryListings = craft.entries({
      section: 'exhibitors',
      relatedTo: ['and',
        { sourceElement: entry, field: 'myEntriesField' },
        { targetElement: category, field: 'myCategoriesField' }
      ]
    }) %}
  
    {# Only show this category if there were any related exhibitors #}
    {% if categoryListings|length %}
  
      &lt;h2&gt;{{ category.title }}&lt;/h2&gt;
  
      {% for listing in categoryListings %}
        &lt;h3&gt;{{ listing.title}}&lt;/h3&gt;
      {% endfor %}
  
    {% endif %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-02T16:26:26.013Z
Answers.Text: Thank you! I tested with this, but it is giving me the same result. I'm getting ALL of the Exhibitors, instead of just those selected in the Event entry via myEntriesField :/
Answers.UserDisplayName: ByteMyPixel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-03T10:22:11.990Z
Answers.Text: Thanks for fixing it, Brad. I should have tested that code before I posted. ;)
Answers.UserDisplayName: Alex Roper
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-03T11:46:41.260Z
Answers.Text: @AlexRoper no prob, that was a weird one, because `entry.myEntriesField` is an ElementCriteriaModel that already has a `relatedTo` param set on it, so your code was actually overriding it.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-06T16:23:00.290Z
Answers.Text: You guys rock!!!  Thanks so much, this works perfectly :)
Answers.UserDisplayName: ByteMyPixel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-02T06:22:04.080Z
Answers.LastActivityDate: 2017-03-03T00:08:29.030Z
Answers.LastEditDate: 2017-03-03T00:08:29.030Z
Answers.UserDisplayName: Alex Roper
Answers.Body: <p>Craft’s <a href="https://craftcms.com/docs/templating/filters#group" rel="nofollow noreferrer"><code>|group</code></a> filter can be used here.</p>
  
  <p>I’m going to assume that the Categories custom field on your Exhibitor entries is called <code>myCategoriesField</code>.</p>
  
  
  
  <pre class="lang-twig prettyprint-override"><code>{% set groupedListings = entry.myEntriesField.find()|group('myCategoriesField.first.title')
  </code></pre>
  
  <p>That will give you an array of grouped Exhibitor entries, grouped by their related category titles. For example, it might look like:</p>
  
  <pre class="lang-twig prettyprint-override"><code>[
      'Category A Title' =&gt; [
          ExhibitorEntry1,
          ExhibitorEntry2
      'Category B Title' =&gt; [
          ExhibitorEntry3,
          ExhibitorEntry4
  ]
  </code></pre>
  
  <p>Then you can loop through the grouped entries like so:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% for categoryTitle, listings in groupedListings %}
      &lt;div class="section listings"&gt;
          &lt;h2&gt;{{ categoryTitle }}&lt;/h2&gt;
          &lt;div class="the-listings"&gt;
              {% for listing in listings %}
                  &lt;div class="item"&gt;
                      &lt;h3&gt;{{ listing.title }}&lt;/h3&gt;
                      ...more entry data stuff...
                  &lt;/div&gt;
              {% endfor %}
          &lt;/div&gt;
      &lt;/div&gt;
  {% endfor %}
  </code></pre>
  
  <p>If you want to sort the category titles alphabetically, it gets slightly more complicated, but still doable:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{# Get the list of category titles from the groupedListings keys, and sort them #}
  {% set sortedCategoryTitles = groupedListings|keys|sort %}
  
  {# Loop through the sorted category titles #}
  {% for categoryTitle in sortedCategoryTitles %}
  
      {# Now get the listings for this category title from the groupedListings array #}
      {% set listings = groupedListings[categoryTitle] %}
  
      {# Now display the category and its listings like normal... #}
      &lt;div class="section listings"&gt;
          &lt;h2&gt;{{ categoryTitle }}&lt;/h2&gt;
          &lt;div class="the-listings"&gt;
              {% for listing in listings %}
                  &lt;div class="item"&gt;
                      &lt;h3&gt;{{ listing.title }}&lt;/h3&gt;
                      ...more entry data stuff...
                  &lt;/div&gt;
              {% endfor %}
          &lt;/div&gt;
      &lt;/div&gt;
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-02T20:30:08.653Z
Answers.Text: Sweet!  That's just what I was needing!  The only thing I forgot to mention though, is some of the Exhibitors are in multiple categories.  But this is only displaying each Exhibitor in only 1 category each. e.g.: Exhibitor A is in Category 1, 2 & 3, but is only showing up in Category 1.
Answers.UserDisplayName: ByteMyPixel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-03T00:09:04.053Z
Answers.Text: @ByteMyPixel Ah, in that case, Alex's solution is better. I just tweaked it a bit - should work for you now.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-02T18:58:32.990Z
Answers.LastActivityDate: 2017-03-02T18:58:32.990Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I have a section called "Events" and an Entries field, we'll call <code>myEntriesField</code> to select related entries from another channel called "Exhibitors"</p>
  
  <p>On the Events page, I need to display the related Exhibitors grouped by their categories. And if there are no related Exhibitors in said Event, I need to hide the surrounding html that displays the category title etc.</p>
  
  <p>I have this in my template:</p>
  
  <pre><code>        {% set theListings = entry.myEntriesField %}
  
  
  
          {% set thisCategory = 
               theListings.relatedTo(craft.categories.slug('the-category-i-need')) 
          %}
  
          {% if thisCategory | length %}
          &lt;div class="section listings"&gt;
              &lt;h2&gt;Category Title&lt;/h2&gt;
              &lt;div class="the-listings"&gt;
              {% for theEntry in thisCategory %}
                  &lt;div class="item"&gt;
                      &lt;h3&gt;{{ theEntry.title }}&lt;/h3&gt;
                      ...more entry data stuff...
                      &lt;/div&gt;
                  &lt;/div&gt;
              {% endfor %}
              &lt;/div&gt;
          &lt;/div&gt;
          {% endif %}
  
  
  
          {% set thisOtherCategory = 
               theListings.relatedTo(craft.categories.slug('the-next-category-i-need')) 
          %}
  
          {% if thisOtherCategory | length %}
          &lt;div class="section listings"&gt;
              &lt;h2&gt;Category Title&lt;/h2&gt;
              &lt;div class="the-listings"&gt;
              {% for otherEntry in thisOtherCategory %}
                  &lt;div class="item"&gt;
                      &lt;h3&gt;{{ otherEntry.title }}&lt;/h3&gt;
                      ...more entry data stuff...
                      &lt;/div&gt;
                  &lt;/div&gt;
              {% endfor %}
              &lt;/div&gt;
          &lt;/div&gt;
          {% endif %}
  </code></pre>
  
  <p>But this displaying ALL Exhibitors, grouped by their categories, rather than just the related Exhibitors selected in the Event entry. </p>
  
  <p>Any ideas what I am missing here?</p>
  
Tags: <categories><conditional><set><relatedto>
Title: How can I filter selected/related entries by their categories?
ThreadText: # How can I filter selected/related entries by their categories?
  
  (Asked by: username_0 on 2017-03-01)
  
  I have a section called "Events" and an Entries field, we'll call `myEntriesField` to select related entries from another channel called "Exhibitors"
  
  On the Events page, I need to display the related Exhibitors grouped by their categories. And if there are no related Exhibitors in said Event, I need to hide the surrounding html that displays the category title etc.
  
  I have this in my template:
  
  ```
          {% set theListings = entry.myEntriesField %}
  
          {% set thisCategory = 
               theListings.relatedTo(craft.categories.slug('the-category-i-need')) 
          %}
  
          {% if thisCategory | length %}
          <div class="section listings">
              <h2>Category Title</h2>
              <div class="the-listings">
              {% for theEntry in thisCategory %}
                  <div class="item">
                      <h3>{{ theEntry.title }}</h3>
                      ...more entry data stuff...
                      </div>
                  </div>
              {% endfor %}
              </div>
          </div>
          {% endif %}
  
          {% set thisOtherCategory = 
               theListings.relatedTo(craft.categories.slug('the-next-category-i-need')) 
          %}
  
          {% if thisOtherCategory | length %}
          <div class="section listings">
              <h2>Category Title</h2>
              <div class="the-listings">
              {% for otherEntry in thisOtherCategory %}
                  <div class="item">
                      <h3>{{ otherEntry.title }}</h3>
                      ...more entry data stuff...
                      </div>
                  </div>
              {% endfor %}
              </div>
          </div>
          {% endif %}
  
  ```
  
  But this displaying ALL Exhibitors, grouped by their categories, rather than just the related Exhibitors selected in the Event entry.
  
  Any ideas what I am missing here?
  
  **Tags:** categories, conditional, set, relatedto
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-03-02. Score: 3 | Accepted answer
  
  Start by looping thru each category. Say, all categories in a category group called "eventsCategories." Then find entries related to each category (if any).
  
  ```twig
  {# loop through each of the categories #}
  {% for category in craft.categories.group('eventsCategories') %}
  
    {# find the exhibitor entries that are related to both the Event entry and category #}
    {% set categoryListings = craft.entries({
      section: 'exhibitors',
      relatedTo: ['and',
        { sourceElement: entry, field: 'myEntriesField' },
        { targetElement: category, field: 'myCategoriesField' }
      ]
    }) %}
  
    {# Only show this category if there were any related exhibitors #}
    {% if categoryListings|length %}
  
      <h2>{{ category.title }}</h2>
  
      {% for listing in categoryListings %}
        <h3>{{ listing.title}}</h3>
      {% endfor %}
  
    {% endif %}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you! I tested with this, but it is giving me the same result. I'm getting ALL of the Exhibitors, instead of just those selected in the Event entry via myEntriesField :/  
  > **username_1**: Thanks for fixing it, Brad. I should have tested that code before I posted. ;)  
  > **username_2**: @username_1 no prob, that was a weird one, because `entry.myEntriesField` is an ElementCriteriaModel that already has a `relatedTo` param set on it, so your code was actually overriding it.  
  > **username_0**: You guys rock!!!  Thanks so much, this works perfectly :)  
  
  ---
  
  ### Answer by username_2 on 2017-03-02. Score: 2
  
  Craft’s [`|group`](https://craftcms.com/docs/templating/filters#group) filter can be used here.
  
  I’m going to assume that the Categories custom field on your Exhibitor entries is called `myCategoriesField`.
  
  ```twig
  {% set groupedListings = entry.myEntriesField.find()|group('myCategoriesField.first.title')
  
  ```
  
  That will give you an array of grouped Exhibitor entries, grouped by their related category titles. For example, it might look like:
  
  ```twig
  [
      'Category A Title' => [
          ExhibitorEntry1,
          ExhibitorEntry2
      'Category B Title' => [
          ExhibitorEntry3,
          ExhibitorEntry4
  ]
  
  ```
  
  Then you can loop through the grouped entries like so:
  
  ```twig
  {% for categoryTitle, listings in groupedListings %}
      <div class="section listings">
          <h2>{{ categoryTitle }}</h2>
          <div class="the-listings">
              {% for listing in listings %}
                  <div class="item">
                      <h3>{{ listing.title }}</h3>
                      ...more entry data stuff...
                  </div>
              {% endfor %}
          </div>
      </div>
  {% endfor %}
  
  ```
  
  If you want to sort the category titles alphabetically, it gets slightly more complicated, but still doable:
  
  ```twig
  {# Get the list of category titles from the groupedListings keys, and sort them #}
  {% set sortedCategoryTitles = groupedListings|keys|sort %}
  
  {# Loop through the sorted category titles #}
  {% for categoryTitle in sortedCategoryTitles %}
  
      {# Now get the listings for this category title from the groupedListings array #}
      {% set listings = groupedListings[categoryTitle] %}
  
      {# Now display the category and its listings like normal... #}
      <div class="section listings">
          <h2>{{ categoryTitle }}</h2>
          <div class="the-listings">
              {% for listing in listings %}
                  <div class="item">
                      <h3>{{ listing.title }}</h3>
                      ...more entry data stuff...
                  </div>
              {% endfor %}
          </div>
      </div>
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Sweet!  That's just what I was needing!  The only thing I forgot to mention though, is some of the Exhibitors are in multiple categories.  But this is only displaying each Exhibitor in only 1 category each. e.g.: Exhibitor A is in Category 1, 2 & 3, but is only showing up in Category 1.  
  > **username_2**: @username_0 Ah, in that case, username_1's solution is better. I just tweaked it a bit - should work for you now.  
  
  ---
  
  