Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-20T18:04:43.993Z
Answers.Text: This is not very effective as it will result in one db query for each category.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-20T15:48:08.347Z
Answers.LastActivityDate: 2015-07-20T15:48:08.347Z
Answers.UserDisplayName: Nick F
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-07T10:49:27.683Z
Answers.LastActivityDate: 2015-07-07T10:49:27.683Z
Answers.UserDisplayName: Justin K
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-17T17:42:15.903Z
Answers.Text: Yeah, this works really well and is easy to understand.
Answers.UserDisplayName: Justin K
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-19T07:01:43.733Z
Answers.Text: 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/
Answers.UserDisplayName: Jérôme Coupé
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-20T16:10:13.900Z
Answers.LastActivityDate: 2015-07-20T16:10:13.900Z
Answers.UserDisplayName: Jérôme Coupé
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-07-31T20:02:02.937Z
Comments.Text: If you got a solution to your problem, please consider accepting one of the answers below
Comments.UserDisplayName: Jérôme Coupé
Tags: <categories>
Title: Trouble getting a list of categories that have entries for a specific category group
ThreadText: # 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?](https://craftcms.stackexchange.com/questions/6762/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 %}
      <h3>{{ handshapeCategory }}</h3>
  {% 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:
  
  1. get all entries in whatever channel
  2. 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:
  
  ```
  <ul>
    {% for handshapeCategory in craft.categories.group('handshape') %}
      {% if craft.entries.relatedTo(handShapeCategory).limit(1).getOne() is not empty %}
        <li>{{ category.title }}</li>
      {% endif %}
    {% endfor %}
  </ul>
  
  ```
  
  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') %}
  
  <h2>List of News Categories in Use:</h2>
  <ul>
      {% for category in newsCategoriesInUse %}
          <li>{{ category.title }}</li>
      {% endfor %}
  </ul>
  
  ```
  
  
  ---
  
  