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

<p>If you <em>only</em> want the total, and aren’t going to be looping through them, use <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel#total" rel="noreferrer">.total()</a>:</p>



<pre class="lang-twig prettyprint-override"><code>{% set entryCount = craft.entries.section('products').relatedTo(category).total() %}
</code></pre>

<p><em>(Note that’s just <code>.total()</code>, not <code>.find().total()</code>.)</em></p>

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

<p>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 <a href="http://twig.sensiolabs.org/doc/filters/length.html" rel="noreferrer">|length</a> filter:</p>

<pre class="lang-twig prettyprint-override"><code>{% set entries = craft.entries.section('products').relatedTo(category) %}
{% set entryCount = entries|length %}

{% for entry in entries %}
    ...
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-10-22T18:38:27.703Z
2014-10-22T18:38:27.703Z
Brandon Kelly
<p>What you're probably looking for is:</p>

<pre><code>{% set entryCount = craft.entries.section('products').relatedTo(category).find()|length %}
</code></pre>

CC BY-SA 3.0
2014-10-20T18:37:27.620Z
2014-10-20T18:37:27.620Z
Brad Bell
<p>I found that the following works (removing the .find() from my initial attempt.)</p>

<pre><code>{% set entryCount = craft.entries.section('products').relatedTo(category).total() %}
</code></pre>

CC BY-SA 3.0
2014-10-20T18:26:32.080Z
2014-10-22T18:39:07.833Z
2014-10-22T18:39:07.833Z
Taylermade
<p>I'm trying to get the total number of entires that match a .relatedTo() search.   </p>

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

<p>I have tried ...</p>

<pre><code>{% set entryCount = craft.entries.section('products').relatedTo(category).find().total() %}
</code></pre>

<p>But no joy. What Am I missing?</p>

<entries>
Outputting .relatedTo() total entries number
# Outputting .relatedTo() total entries number

(Asked by: username_0 on 2014-10-20)

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?

**Tags:** entries

---

## 3 answers

### Answer by username_1 on 2014-10-22. Score: 8

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 %}


---

### Answer by username_2 on 2014-10-20. Score: 4

What you're probably looking for is:
{% set entryCount = craft.entries.section('products').relatedTo(category).find()|length %}


---

### Answer by username_0 on 2014-10-20. Score: 4

I found that the following works (removing the .find() from my initial attempt.)
{% set entryCount = craft.entries.section('products').relatedTo(category).total() %}


---

