<p>I don't know how it compares efficiency-wise, but another approach to this (which I personally find much easier to read) would be something like:</p>

<pre><code>&lt;ul&gt;
  {% for handshapeCategory in craft.categories.group('handshape') %}
    {% if craft.entries.relatedTo(handShapeCategory).limit(1).getOne() is not empty %}
      &lt;li&gt;{{ category.title }}&lt;/li&gt;
    {% endif %}
  {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>In other words, for each category in the group, if we can fetch an entry for that category, display the title.</p>

CC BY-SA 3.0
2015-07-20T18:04:43.993Z
This is not very effective as it will result in one db query for each category.
carlcs
CC BY-SA 3.0
2015-07-20T15:48:08.347Z
2015-07-20T15:48:08.347Z
Nick F
<p>In order to limit the related categories to a specific category group I needed to add <code>.group('desiredCategoryGroup')</code> to the last line.</p>

<p>Here's an example of what I came up with:</p>

<pre><code>{% set newsCategories = craft.categories.group('news').limit(null) %}
{% set soriesWithNewsCategory = craft.entries.relatedTo( newsCategories ).limit(null) %}
{% set newsCategoriesInUse = craft.categories.relatedTo(soriesWithNewsCategory).group('news') %}

&lt;h2&gt;List of News Categories in Use:&lt;/h2&gt;
&lt;ul&gt;
    {% for category in newsCategoriesInUse %}
        &lt;li&gt;{{ category.title }}&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
</code></pre>

CC BY-SA 3.0
2015-07-07T10:49:27.683Z
2015-07-07T10:49:27.683Z
Justin K
<p>I would reverse your logic:</p>

<ol>
<li>get all entries in whatever channel</li>
<li>display only categories (from a certain group) that have entries fetched in step 1 attached to them</li>
</ol>

<p>Something along the lines of:</p>

<pre><code>{% set entries = craft.entries.section('myChannel').limit(null) %}
{% set activeCategories = craft.categories.group('myCatgroup').relatedTo(entries).find() %}
</code></pre>

<p>You can then use a simple <code>for</code> loop to display only the active categories</p>

CC BY-SA 3.0
2015-08-17T17:42:15.903Z
Yeah, this works really well and is easy to understand.
Justin K
CC BY-SA 3.0
2015-08-19T07:01:43.733Z
Glad it helps. You might also be interested in a more advanced set of tricks I blogged about: http://webstoemp.com/blog/manipulating-craft-elementcriteriamodel-with-twig/
Jérôme Coupé
CC BY-SA 3.0
2015-07-20T16:10:13.900Z
2015-07-20T16:10:13.900Z
Jérôme Coupé
<p>I have a long list of categories in a specific category group, but only want to show a list of the categories from this group that currently have an entry associated with them.</p>

<p>I've been trying to implement the solution that I found at this thread: <a href="https://craftcms.stackexchange.com/questions/6762/how-can-i-output-a-list-of-categories-that-are-not-empty">How can I output a list of categories that are not empty?</a></p>

<p>Only categories with related entries are showing. However, it's not being narrowed down to specific to a specific category group. Here's my code below:</p>

<pre><code>{% set handshapeCategories = craft.categories.group('handshape').limit(null) %}
{% set entriesWithHandshapeCategories = craft.entries.relatedTo( handshapeCategories ).limit(null) %}
{% set handshapeCategoriesInUse = craft.categories.relatedTo(entriesWithHandshapeCategories) %}

{% for handshapeCategory in handshapeCategoriesInUse %}
    &lt;h3&gt;{{ handshapeCategory }}&lt;/h3&gt;
{% endfor %}       
</code></pre>

<p>Any ideas why the shown categories are not being limited to categories from the 'handshape' category group?</p>

CC BY-SA 3.0
2015-07-31T20:02:02.937Z
If you got a solution to your problem, please consider accepting one of the answers below
Jérôme Coupé
<categories>
Trouble getting a list of categories that have entries for a specific category group
# Trouble getting a list of categories that have entries for a specific category group

(Asked by: username_0 on 2015-07-06)

I have a long list of categories in a specific category group, but only want to show a list of the categories from this group that currently have an entry associated with them.
I've been trying to implement the solution that I found at this thread: How can I output a list of categories that are not empty?
Only categories with related entries are showing. However, it's not being narrowed down to specific to a specific category group. Here's my code below:
{% set handshapeCategories = craft.categories.group('handshape').limit(null) %}
{% set entriesWithHandshapeCategories = craft.entries.relatedTo( handshapeCategories ).limit(null) %}
{% set handshapeCategoriesInUse = craft.categories.relatedTo(entriesWithHandshapeCategories) %}

{% for handshapeCategory in handshapeCategoriesInUse %}
    {{ handshapeCategory }}
{% endfor %}       

Any ideas why the shown categories are not being limited to categories from the 'handshape' category group?

**Tags:** categories
> **Comments:**
> **username_1**: If you got a solution to your problem, please consider accepting one of the answers below  

---

## 3 answers

### Answer by username_1 on 2015-07-20. Score: 5 | Accepted answer

I would reverse your logic:

get all entries in whatever channel
display only categories (from a certain group) that have entries fetched in step 1 attached to them

Something along the lines of:
{% set entries = craft.entries.section('myChannel').limit(null) %}
{% set activeCategories = craft.categories.group('myCatgroup').relatedTo(entries).find() %}

You can then use a simple for loop to display only the active categories

> **Comments:**
> **username_0**: Yeah, this works really well and is easy to understand.  
> **username_1**: Glad it helps. You might also be interested in a more advanced set of tricks I blogged about: http://webstoemp.com/blog/manipulating-craft-elementcriteriamodel-with-twig/  

---

### Answer by username_2 on 2015-07-20. Score: 1

I don't know how it compares efficiency-wise, but another approach to this (which I personally find much easier to read) would be something like:

  {% for handshapeCategory in craft.categories.group('handshape') %}
    {% if craft.entries.relatedTo(handShapeCategory).limit(1).getOne() is not empty %}
      {{ category.title }}
    {% endif %}
  {% endfor %}


In other words, for each category in the group, if we can fetch an entry for that category, display the title.

> **Comments:**
> **username_3**: This is not very effective as it will result in one db query for each category.  

---

### Answer by username_0 on 2015-07-07. Score: 1

In order to limit the related categories to a specific category group I needed to add .group('desiredCategoryGroup') to the last line.
Here's an example of what I came up with:
{% set newsCategories = craft.categories.group('news').limit(null) %}
{% set soriesWithNewsCategory = craft.entries.relatedTo( newsCategories ).limit(null) %}
{% set newsCategoriesInUse = craft.categories.relatedTo(soriesWithNewsCategory).group('news') %}

List of News Categories in Use:

    {% for category in newsCategoriesInUse %}
        {{ category.title }}
    {% endfor %}


---

