<p>Have you read the docs about <a href="https://docs.craftcms.com/v2/relations.html#the-relatedTo-param" rel="nofollow noreferrer">relations</a>? </p>

<pre><code>'criteria' =&gt; [
    'section' =&gt; 'projects', 
    'test' =&gt; 'testValue' 
    'relatedTo' =&gt; id
]
</code></pre>

<p>Just replace <code>id</code> with the ID of your element </p>

<p>If you want to filter a specific field you have to include it as an array </p>

<pre><code>'relatedTo' =&gt; [
    'sourceElement' =&gt; id,
    'field' =&gt; 'fieldHandle'
] 
</code></pre>

CC BY-SA 3.0
2018-04-25T07:23:02.250Z
Indeed, `relatedTo` was the missing key! Thanks a lot.
svrbst
CC BY-SA 3.0
2018-04-24T18:07:25.537Z
2018-04-24T18:12:39.793Z
2018-04-24T18:12:39.793Z
Robin Schambach
<p>I have a channel called 'projects'. 
This channel has two <code>entries fields</code>:</p>

<ul>
<li>relatedThemes</li>
<li>relatedAreas</li>
</ul>

<p>What I want to do is perform a search query which filters all projects where the <code>relatedThemes</code> field has an entry with a certain <code>id</code>.</p>

<p>I figured out how to search custom fields. Here's an example of a filter with a plain text field with a handle <code>test</code>.</p>

<pre><code>{% set entries = craft.entries({
  section: 'projects'
}).search('test:testValue') %}
</code></pre>

<p>This returns all project entries where the <code>test</code> field has the <code>testValue</code> value, as expected.</p>

<p>But I can't seem to make this work with an entries field. I thought I could do something like this</p>

<pre><code>{% set entries = craft.entries({
  section: 'projects'
}).search('entriesfield.id:relatedThemesID') %}
</code></pre>

<p>But this does not work.</p>

<p>Another important thing is that I <strong><em>can't actually use template logic</em></strong>, because the query is to be used in the <a href="https://github.com/craftcms/element-api" rel="nofollow noreferrer">element api</a> (for Craft 2).</p>

<p>I'm not a backend dev, so I test the <code>criteria</code> array in templates first and then convert it to php. </p>

<p>So the correct criteria array for the test text field would be</p>

<pre><code>'criteria' =&gt; [
  'section' =&gt; 'projects',
  'test' =&gt; 'testValue'
],
</code></pre>

CC BY-SA 3.0
2018-04-26T09:04:56.377Z
If Robin Schambach's answer was the one that helped you fix this consider checking this answer as the correct one. It helps others on here to find answers faster.
xaddict
CC BY-SA 3.0
2018-04-27T07:49:04.997Z
Done @xaddict .
Pretty new to stackexchange, so thanks for the tip!
svrbst
CC BY-SA 3.0
2018-04-28T09:32:59.670Z
No problem 
xaddict
<elementcriteriamodel><plugin-elementapi><query>
Get all entries with linked entry in entries field
# Get all entries with linked entry in entries field

(Asked by: username_0 on 2018-04-24)

I have a channel called 'projects'. 
This channel has two entries fields:

relatedThemes
relatedAreas

What I want to do is perform a search query which filters all projects where the relatedThemes field has an entry with a certain id.
I figured out how to search custom fields. Here's an example of a filter with a plain text field with a handle test.
{% set entries = craft.entries({
  section: 'projects'
}).search('test:testValue') %}

This returns all project entries where the test field has the testValue value, as expected.
But I can't seem to make this work with an entries field. I thought I could do something like this
{% set entries = craft.entries({
  section: 'projects'
}).search('entriesfield.id:relatedThemesID') %}

But this does not work.
Another important thing is that I can't actually use template logic, because the query is to be used in the element api (for Craft 2).
I'm not a backend dev, so I test the criteria array in templates first and then convert it to php. 
So the correct criteria array for the test text field would be
'criteria' => [
  'section' => 'projects',
  'test' => 'testValue'
],

**Tags:** elementcriteriamodel, plugin-elementapi, query
> **Comments:**
> **username_1**: If Robin Schambach's answer was the one that helped you fix this consider checking this answer as the correct one. It helps others on here to find answers faster.  
> **username_0**: Done @xaddict . Pretty new to stackexchange, so thanks for the tip!  
> **username_1**: No problem   

---

## 1 answers

### Answer by username_2 on 2018-04-24. Score: 1 | Accepted answer

Have you read the docs about relations? 
'criteria' => [
    'section' => 'projects', 
    'test' => 'testValue' 
    'relatedTo' => id
]

Just replace id with the ID of your element 
If you want to filter a specific field you have to include it as an array 
'relatedTo' => [
    'sourceElement' => id,
    'field' => 'fieldHandle'
]

> **Comments:**
> **username_0**: Indeed, `relatedTo` was the missing key! Thanks a lot.  

---

