<p>Second <code>relatedTo</code> call overrides first one. Calling it again won't add more parameters to the query.</p>

<p>If you don't want to repeat yourself you can always do this:</p>

<pre><code>{% set creatorRelated = { targetElement: creatorEntry, field: 'creator' } %}

{% set caseEntries = craft.entries.section('case').relatedTo(creatorRelated) %}

{% set caseEntries = caseEntries.relatedTo('and', creatorRelated, { targetElement: categoryEntry, field: 'caseCategories' }) %}
</code></pre>

CC BY-SA 3.0
2016-11-15T10:26:55.273Z
2016-11-15T10:26:55.273Z
Krzysztof Boduch
<p>I have a question about relations... or maybe its also about making queries. First I want to select a set of data, and then I want to select a subset of that data, based on a relatedTo call.</p>

<p>If I first do this:</p>

<pre><code>{% set caseEntries = craft.entries.section('case').relatedTo({ targetElement: creatorEntry, field: 'creator' }) %}
</code></pre>

<p>then, to get a sub-set of what I got before, do this:</p>

<pre><code>{% set caseEntries = caseEntries.relatedTo('and', { targetElement: categoryEntry, field: 'caseCategories' }) %}
</code></pre>

<p>it seems as if the first relation is “forgotten” (note that it wont work if I dont include an “and” in the second related to call). Do I <em>have to</em> add the first relation in the second call? Feels like repeating myself... I want to avoid doing this:</p>

<pre><code>{% set caseEntries = caseEntries.relatedTo('and', { targetElement: creatorEntry, field: 'creator' }, { targetElement: categoryEntry, field: 'caseCategories' }) %}
</code></pre>

<entries><relatedto>
Selecting a subset of entries, based on a relatedTo call
# Selecting a subset of entries, based on a relatedTo call

(Asked by: username_0 on 2016-11-15)

I have a question about relations... or maybe its also about making queries. First I want to select a set of data, and then I want to select a subset of that data, based on a relatedTo call.
If I first do this:
{% set caseEntries = craft.entries.section('case').relatedTo({ targetElement: creatorEntry, field: 'creator' }) %}

then, to get a sub-set of what I got before, do this:
{% set caseEntries = caseEntries.relatedTo('and', { targetElement: categoryEntry, field: 'caseCategories' }) %}

it seems as if the first relation is “forgotten” (note that it wont work if I dont include an “and” in the second related to call). Do I have to add the first relation in the second call? Feels like repeating myself... I want to avoid doing this:
{% set caseEntries = caseEntries.relatedTo('and', { targetElement: creatorEntry, field: 'creator' }, { targetElement: categoryEntry, field: 'caseCategories' }) %}

**Tags:** entries, relatedto

---

## 1 answers

### Answer by username_1 on 2016-11-15. Score: 3 | Accepted answer

Second relatedTo call overrides first one. Calling it again won't add more parameters to the query.
If you don't want to repeat yourself you can always do this:
{% set creatorRelated = { targetElement: creatorEntry, field: 'creator' } %}

{% set caseEntries = craft.entries.section('case').relatedTo(creatorRelated) %}

{% set caseEntries = caseEntries.relatedTo('and', creatorRelated, { targetElement: categoryEntry, field: 'caseCategories' }) %}


---

