<p>You could get the entries ids, then get the categories related to those entries.
Like so:</p>

<pre><code>{%- set ids= craft.entries.section('sectionHandle').ids() -%}

{%- for cat in craft.categories.relatedTo({ sourceElement: ids }).group('categoryHandle') -%}
    {{- cat.title -}}
{%- endfor -%}
</code></pre>

<p>That will only print categories with an entry associated with.</p>

CC BY-SA 4.0
2019-02-01T19:56:55.977Z
Here's a video that covers a similar approach and also includes adding the entry count with each category. https://craftquest.io/lessons/creating-a-dynamic-category-list
CraftQuest
CC BY-SA 4.0
2019-02-01T18:20:36.193Z
2019-02-01T18:20:36.193Z
Cryptomothy
<p>I have a set of categories that I would like to display on a sidebar as a way to filter the list of entries displayed. However, some of those categories haven't been associated with any entries yet so clicking on that category with return 0 results. 
What is the most efficient way to exclude those categories?</p>

<craft3>
Exclude categories that are not associated to any entry
# Exclude categories that are not associated to any entry

(Asked by: username_0 on 2019-02-01)

I have a set of categories that I would like to display on a sidebar as a way to filter the list of entries displayed. However, some of those categories haven't been associated with any entries yet so clicking on that category with return 0 results.
What is the most efficient way to exclude those categories?

**Tags:** craft3


---

## 1 answers

### Answer by username_1 on 2019-02-01. Score: 1 | Accepted answer

You could get the entries ids, then get the categories related to those entries.
Like so:
{%- set ids= craft.entries.section('sectionHandle').ids() -%}

{%- for cat in craft.categories.relatedTo({ sourceElement: ids }).group('categoryHandle') -%}
    {{- cat.title -}}
{%- endfor -%}

That will only print categories with an entry associated with.

> **Comments:**
> **username_2**: Here's a video that covers a similar approach and also includes adding the entry count with each category. https://craftquest.io/lessons/creating-a-dynamic-category-list  

---

