This is from a template that worked in Craft 2, but doesn't in Craft 3.

{% set entries = craft.entries.type('publications').orderBy("postDate desc") %}

entries.count() outputs 80

{% for cat in allCats %}
    {% set catEntries = entries.relatedTo(cat) %}
    {% if catEntries | length or cat.slug=="all" or cat.slug=="toutes" %}
        <li><a href="{{ cat.url }}" {% if category is defined and cat.slug == category.slug %}class="current"{% endif %}>{{ cat.title }}</a></li>
    {% endif %}
{% endfor %}

This iterates through all categories with correct entries associated

After that, entries.count() outputs 0

Looks like entries is getting set to the last value instead of just passing the resulting object to the variable. This is not how it worked before. Is there a way to reset this ?


1 Answer 1


Based on the bug report found here (https://github.com/craftcms/cms/issues/2160), I am now creating a variable that contains all of the items prior to filtering it.

{% set entries = craft.entries.type('publications').orderBy("postDate desc") %}
{% set allEntries = entries.all() %}

I can then use entries to filter through the available categories and then use allEntries to loop through all of them.

I understand I could also have used clone() to get a copy to work from in my category loop.

{% set catEntries = clone(entries).relatedTo(cat) %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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