<p>I'm using categories and relating them to entries.</p>

<p>If using <code>.relatedTo().total()</code> I'm seeing the total number of ALL entries if none of the entries are related to a particular category.</p>

<p>If using <code>.relatedTo().find()|length</code> then zero is returned as expected.</p>

<p>I'm trying to output a link list of all categories with their total related entries IF they have any entries.</p>

<p>Here is the code I'm using (with a testing snippet to show how <code>|length</code> is working.</p>

<pre><code>{% set cats = craft.categories.group('topic') %}
{% for cat in cats %}
  {% set total = typeEntries.relatedTo({targetElement:cat}).total() %}
  {% if total %}
    &lt;li&gt;&lt;a href="{{ urlPrefix ~ '/topic:' ~ cat.slug }}"&gt;{{ cat.title }} ({{ total }})&lt;/a&gt;&lt;/li&gt;
    testing length: {{ typeEntries.relatedTo({targetElement:cat}).find()|length }}
  {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-02-09T20:56:20.057Z
Strange. I'm getting the same result for both `.total()` and `|length` (with or without the `find()`).
Douglas McDonald
CC BY-SA 3.0
2015-02-10T17:50:43.260Z
I know, it's bizarre. Must be some kind of strange edge case... or maybe it's because this is my first Craft site :-). For now I'm just using `.find()|length` so it displays as expected.
Kirk Roberts
<entries><categories><relations>
Is .relatedTo().total() returning all source elements if none are found?
# Is .relatedTo().total() returning all source elements if none are found?

(Asked by: username_0 on 2015-02-09)

I'm using categories and relating them to entries.
If using .relatedTo().total() I'm seeing the total number of ALL entries if none of the entries are related to a particular category.
If using .relatedTo().find()|length then zero is returned as expected.
I'm trying to output a link list of all categories with their total related entries IF they have any entries.
Here is the code I'm using (with a testing snippet to show how |length is working.
{% set cats = craft.categories.group('topic') %}
{% for cat in cats %}
  {% set total = typeEntries.relatedTo({targetElement:cat}).total() %}
  {% if total %}
    {{ cat.title }} ({{ total }})
    testing length: {{ typeEntries.relatedTo({targetElement:cat}).find()|length }}
  {% endif %}
{% endfor %}

**Tags:** entries, categories, relations

> **Comments:**
> **username_1**: Strange. I'm getting the same result for both `.total()` and `|length` (with or without the `find()`).  
> **username_0**: I know, it's bizarre. Must be some kind of strange edge case... or maybe it's because this is my first Craft site :-). For now I'm just using `.find()|length` so it displays as expected.  

---

## 0 answers

