Caching and related Entries I'm currently working on integration some caching behaviour using {% cache %} tags. This works mostly just fine, but I have some issues when I work with related entries.

For example I have a list of categories with an indicator to show many entries they are assigned to.


New (3)
Used (2)
Broken (3)


If I change an entry and add a category, the cache refreshes and shows the correct amount. But if I remove a category from an entry I get the wrong number until I refresh the cache manually.

Is this the expected behaviour? Or do I miss something here?

Here is my code example:

{% cache unless not craft.config.cache %}
{% set categorySlug = productSection ~ 'Categories' %}

&lt;ul&gt;

{% for category in craft.categories.group(categorySlug) %}
  {% set entryCount = craft.entries.relatedTo(category).total() %}
  {% if entryCount &gt; 0 %}

    &lt;li&gt;
      {{ category.title }} ( {{ entryCount }} )
    &lt;/li&gt;

  {% endif %}
{% endfor %}

&lt;/ul&gt;

{% endcache %}
This sounds like a bug, and I think I know why. Can you try changing the {% set entryCount ... line to this?

{% set entryCount = craft.entries.relatedTo(category.id).total() %}