<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>

CC BY-SA 3.0
2017-03-02T16:26:26.013Z
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 :/
ByteMyPixel
CC BY-SA 3.0
2017-03-03T10:22:11.990Z
Thanks for fixing it, Brad. I should have tested that code before I posted. ;)
Alex Roper
CC BY-SA 3.0
2017-03-03T11:46:41.260Z
@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.
Brandon Kelly
CC BY-SA 3.0
2017-03-06T16:23:00.290Z
You guys rock!!!  Thanks so much, this works perfectly :)
ByteMyPixel
CC BY-SA 3.0
2017-03-02T06:22:04.080Z
2017-03-03T00:08:29.030Z
2017-03-03T00:08:29.030Z
Alex Roper
<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>

CC BY-SA 3.0
2017-03-02T20:30:08.653Z
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.
ByteMyPixel
CC BY-SA 3.0
2017-03-03T00:09:04.053Z
@ByteMyPixel Ah, in that case, Alex's solution is better. I just tweaked it a bit - should work for you now.
Brandon Kelly
CC BY-SA 3.0
2017-03-02T18:58:32.990Z
2017-03-02T18:58:32.990Z
Brandon Kelly
<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>

<categories><conditional><set><relatedto>
How can I filter selected/related entries by their categories?
# 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 %}
        
            Category Title
            
            {% for theEntry in thisCategory %}
                
                    {{ theEntry.title }}
                    ...more entry data stuff...
                    
                
            {% endfor %}
            
        
        {% endif %}



        {% set thisOtherCategory = 
             theListings.relatedTo(craft.categories.slug('the-next-category-i-need')) 
        %}

        {% if thisOtherCategory | length %}
        
            Category Title
            
            {% for otherEntry in thisOtherCategory %}
                
                    {{ otherEntry.title }}
                    ...more entry data stuff...
                    
                
            {% endfor %}
            
        
        {% 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).
{# 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 %}

    {{ category.title }}

    {% for listing in categoryListings %}
      {{ listing.title}}
    {% 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**: @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.  
> **username_0**: You guys rock!!!  Thanks so much, this works perfectly :)  

---

### Answer by username_2 on 2017-03-02. Score: 2

Craft’s |group filter can be used here.
I’m going to assume that the Categories custom field on your Exhibitor entries is called myCategoriesField.
{% 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:
[
    'Category A Title' => [
        ExhibitorEntry1,
        ExhibitorEntry2
    'Category B Title' => [
        ExhibitorEntry3,
        ExhibitorEntry4
]

Then you can loop through the grouped entries like so:
{% for categoryTitle, listings in groupedListings %}
    
        {{ categoryTitle }}
        
            {% for listing in listings %}
                
                    {{ listing.title }}
                    ...more entry data stuff...
                
            {% endfor %}
        
    
{% endfor %}

If you want to sort the category titles alphabetically, it gets slightly more complicated, but still doable:
{# 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... #}
    
        {{ categoryTitle }}
        
            {% for listing in listings %}
                
                    {{ listing.title }}
                    ...more entry data stuff...
                
            {% endfor %}
        
    
{% 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**: @ByteMyPixel Ah, in that case, Alex's solution is better. I just tweaked it a bit - should work for you now.  

---

