Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-22T18:38:27.703Z
Answers.LastActivityDate: 2014-10-22T18:38:27.703Z
Answers.UserDisplayName: Brandon Kelly
Answers.Body: <p>What you're probably looking for is:</p>
  
  <pre><code>{% set entryCount = craft.entries.section('products').relatedTo(category).find()|length %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-20T18:37:27.620Z
Answers.LastActivityDate: 2014-10-20T18:37:27.620Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-20T18:26:32.080Z
Answers.LastActivityDate: 2014-10-22T18:39:07.833Z
Answers.LastEditDate: 2014-10-22T18:39:07.833Z
Answers.UserDisplayName: Taylermade
Body: <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>
  
Tags: <entries>
Title: Outputting .relatedTo() total entries number
ThreadText: # 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()](http://buildwithcraft.com/docs/templating/elementcriteriamodel#total):
  
  ```twig
  {% 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](http://twig.sensiolabs.org/doc/filters/length.html) filter:
  
  ```twig
  {% 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() %}
  
  ```
  
  
  ---
  
  