<p>Typically I use an <code>ids()</code> query to do that kind of thing, as it is usually more efficient than a full-blown <code>.all()</code>, like this:</p>
<pre><code>{% set allPartnerIds = craft.entries.section('partners').limit(null).ids() %}
{% set tiersInUse = craft.categories.group('catPartnerTeirs').relatedTo(allPartnerIds).all() %}
</code></pre>
<p>If you have already queried for the partner entries elsewhere in the template, you could avoid any extra queries by using the <code>column</code> filter like this:</p>
<pre><code>{% set allPartnerIds = allPartnerEntries|column('id') %}
{% set tiersInUse = craft.categories.group('catPartnerTeirs').relatedTo(allPartnerIds).all() %}
</code></pre>
<p>(though you should be fine to pass in the actual elements instead of their ids - you just need to ensure that the element query is <strong>executed</strong> (i.e., with <code>.all()</code>).</p>

CC BY-SA 4.0
2022-01-19T11:02:05.333Z
2022-01-19T11:02:05.333Z
James Smith
<p>I'm trying to find all tiers that have associated entries for a selection dropdown.</p>
<p>This does not work, and throws a 502 error</p>
<pre><code>{% set allTeirs = craft.categories.group('catPartnerTeirs') %}
{% set partnersWithTeirs = allPartnerEntries.relatedTo(allTeirs).limit(null) %}
{% set partnerTeirsInUse = allTeirs.relatedTo(partnersWithTeirs).all() %}
</code></pre>
<p>This does work, but results in duplicate DB queries:</p>
<pre><code>{% set allTeirs = craft.categories.group('catPartnerTeirs') %}
{% set partnersWithTeirs = allPartnerEntries.relatedTo(allTeirs).limit(null) %}
{% set partnerTeirsInUse = craft.categories.group('catPartnerTeirs').relatedTo(partnersWithTeirs).all() %}
</code></pre>
<p>Can someone explain to me why? I've dumped them both to the page (the set variable and the query itself and they both return exactly the same object, so far as I can tell.</p>
<p>I'm also totally open to better ways of finding &quot;Tiers that have associated entries&quot;, if you've got that too.</p>

CC BY-SA 4.0
2022-01-18T16:39:41.317Z
Can you add the query for `allPartnerEntries` so we can see what that looks like? `allTeirs` should be a list of categories, `allPartnerEntries` should return a list of entries, `partnerTeirsInUse` should return a list of categories (possibly shorter than `allTeirs`). What do you get?
Clive Portman
<templating><entries><categories>
Bad Gateway when using cached category query
# Bad Gateway when using cached category query

(Asked by: username_0 on 2022-01-17)

I'm trying to find all tiers that have associated entries for a selection dropdown.
This does not work, and throws a 502 error
{% set allTeirs = craft.categories.group('catPartnerTeirs') %}
{% set partnersWithTeirs = allPartnerEntries.relatedTo(allTeirs).limit(null) %}
{% set partnerTeirsInUse = allTeirs.relatedTo(partnersWithTeirs).all() %}

This does work, but results in duplicate DB queries:
{% set allTeirs = craft.categories.group('catPartnerTeirs') %}
{% set partnersWithTeirs = allPartnerEntries.relatedTo(allTeirs).limit(null) %}
{% set partnerTeirsInUse = craft.categories.group('catPartnerTeirs').relatedTo(partnersWithTeirs).all() %}

Can someone explain to me why? I've dumped them both to the page (the set variable and the query itself and they both return exactly the same object, so far as I can tell.
I'm also totally open to better ways of finding "Tiers that have associated entries", if you've got that too.

**Tags:** templating, entries, categories
> **Comments:**
> **username_1**: Can you add the query for `allPartnerEntries` so we can see what that looks like? `allTeirs` should be a list of categories, `allPartnerEntries` should return a list of entries, `partnerTeirsInUse` should return a list of categories (possibly shorter than `allTeirs`). What do you get?  

---

## 1 answers

### Answer by username_2 on 2022-01-19. Score: 1

Typically I use an ids() query to do that kind of thing, as it is usually more efficient than a full-blown .all(), like this:
{% set allPartnerIds = craft.entries.section('partners').limit(null).ids() %}
{% set tiersInUse = craft.categories.group('catPartnerTeirs').relatedTo(allPartnerIds).all() %}

If you have already queried for the partner entries elsewhere in the template, you could avoid any extra queries by using the column filter like this:
{% set allPartnerIds = allPartnerEntries|column('id') %}
{% set tiersInUse = craft.categories.group('catPartnerTeirs').relatedTo(allPartnerIds).all() %}

(though you should be fine to pass in the actual elements instead of their ids - you just need to ensure that the element query is executed (i.e., with .all()).


---

