I'm trying to get the total number of entires that match a .relatedTo() search.

{% set category = craft.categories.group('products').title(cat).first() %}
{% set entries = craft.entries.section('products').relatedTo(category).find() %}

I have tried ...

{% set entryCount = craft.entries.section('products').relatedTo(category).find().total() %}

But no joy. What Am I missing?

3 Answers 3


The best way to do it depends on whether you actually want to loop through the related entries or not.

If you only want the total, and aren’t going to be looping through them, use .total():

{% set entryCount = craft.entries.section('products').relatedTo(category).total() %}

(Note that’s just .total(), not .find().total().)

That way you’re not making Craft do any more work than what you actually need - just figuring out the total related entries.

If you do want to loop through them as well, then grab the entries normally and then figure out how many came back using Twig’s |length filter:

{% set entries = craft.entries.section('products').relatedTo(category) %}
{% set entryCount = entries|length %}

{% for entry in entries %}
{% endfor %}

What you're probably looking for is:

{% set entryCount = craft.entries.section('products').relatedTo(category).find()|length %}

I found that the following works (removing the .find() from my initial attempt.)

{% set entryCount = craft.entries.section('products').relatedTo(category).total() %}

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