0

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'
],
3
  • 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
    Apr 26, 2018 at 9:04
  • Done @xaddict . Pretty new to stackexchange, so thanks for the tip!
    – svrbst
    Apr 27, 2018 at 7:49
  • No problem 👍🏼
    – xaddict
    Apr 28, 2018 at 9:32

1 Answer 1

1

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'
] 
1
  • Indeed, relatedTo was the missing key! Thanks a lot.
    – svrbst
    Apr 25, 2018 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.