Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to list all categories that have an entry assigned to them, but hide the ones that are empty.

In EE I could do this show_empty="no"

But I can't see a parameter in the docs for Craft.

Currently I've found that I could use relatedTo but with passing an object with up to 600 entries it dies with caching tags :(

Is there a better way of doing this? This is what I've got at the moment:

{% set blogPosts = craft.entries({section:'blog',limit:null}).find() %}

{% for cat in craft.categories({groupId:2}).relatedTo(blogPosts) %}
   <li><a href="/blog/{{cat.slug}}">{{cat.title}}</a> </li>
{% endfor %}
share|improve this question
up vote 9 down vote accepted

There is a great tutorial over at Straight Up Craft for handling this (it is however very similar to what you have - it may speed things up a tad though).

First, we want to grab all of the entry ids from the Section of content which we want to display categories for.

{% set entryIds = craft.entries.section('blog').ids() %}

Next, we will use the craft.categories tags relatedTo() method to only return categories that have a relationship with one of our entry ids.

{% set categories = craft.categories.relatedTo({ sourceElement: entryIds }).groupId(1).find() %}

Once we have our list of categories that are in use, we can loop through those categories and display them in our template:

{% for category in categories %}
    <li><a href="{{ category.url }}">{{ category.title }}</a></li>
{% endfor %}
share|improve this answer
Thanks, certainly appears to be faster! – joe Jul 24 '14 at 8:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.