<p>This is a bit brute-force but you can loop all the elements and then check their related count:</p>
<pre><code>{% for row in craft.entries.section('foo').all() %}
   {% if row.myRelatedField.count() == 10 %}
      &lt;li&gt;{{ row.title }}&lt;/li&gt;
   {% endif %}
{% endfor %}
</code></pre>
<p>You may want to eager-load myRelatedField to save an expensive database query.</p>

CC BY-SA 4.0
2021-08-27T11:59:24.523Z
2021-08-27T11:59:24.523Z
gioppe
<p>does anyone know if there's a way to display a type of &quot;most popular categories&quot; list? It's not exactly my use-case, but it's very similar</p>
<p>Essentially, I want to be able to have users search for elements with a certain number of relations. I saw an article about using Preparse but these are being maintained by front-end users and I don't want to have to wait for someone to save the parent element to have an accurate count.</p>

CC BY-SA 4.0
2021-08-27T13:27:01.227Z
Would this work for your case - https://craftcms.stackexchange.com/a/37188/3630 ?
Oli
<templating><relations>
Sorting Categories/Entries by # of relations dynamically
# Sorting Categories/Entries by # of relations dynamically

(Asked by: username_0 on 2021-08-26)

does anyone know if there's a way to display a type of "most popular categories" list? It's not exactly my use-case, but it's very similar

Essentially, I want to be able to have users search for elements with a certain number of relations. I saw an article about using Preparse but these are being maintained by front-end users and I don't want to have to wait for someone to save the parent element to have an accurate count.

**Tags:** templating, relations

> **Comments:**
> **username_1**: Would this work for your case - https://craftcms.stackexchange.com/a/37188/3630 ?  

---

## 1 answers

### Answer by username_2 on 2021-08-27. Score: 1

This is a bit brute-force but you can loop all the elements and then check their related count:
{% for row in craft.entries.section('foo').all() %}
   {% if row.myRelatedField.count() == 10 %}
      {{ row.title }}
   {% endif %}
{% endfor %}

You may want to eager-load myRelatedField to save an expensive database query.


---

