5

My question could seem obvious but I don't find the solution.

How to make a search on two fields, like:

craft.entries.section('pages').search('foo:bar foo2:bar2')

I tried

search('foo:bar', 'foo2:bar4')
search('foo:bar and foo2:bar2')

none of these seem to work

1 Answer 1

6

Your search syntax of craft.entries.section('pages').search('foo:bar foo2:bar2') should work and is the equivalent of saying "give me all of the entries in the pages section where the field foo is equal to bar and the field foo2 is equal to bar2.

Something that trips people up often is that Craft doesn't do fuzzy searching by default. So foo:bar will match exactly bar, but it will not match bars. You'd need to use foo:bar* for that.

3
  • Oh, maybe I'm wrong somewhere else then. My search uses a lightswitch and a position: craft.entries.section('pages').search('onHomePage::true position::"left"') I get the pages who are not "onHomePage" as well
    – Julien
    Jun 12, 2015 at 5:52
  • And it appears for OR searches this works: search('foo:bar OR foo2:bar2')
    – JamesNZ
    Nov 23, 2016 at 21:59
  • 3
    Just to clarify, as foo and bar are petpeeves of mine that I find ... less than helpful; the syntax is .search('fieldHandle:textToFind otherFieldHandle:someOtherText') Jul 4, 2018 at 14:26

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.