Jump to content

Selector AND, OR syntax


Robin S
 Share

Recommended Posts

I'm sure there's a simple answer to this that's staring me in the face but I'm struggling to work out the right combination of pipe separators and OR-groups.

How do I write a selector for the following:

(text_field*=cat OR page_field=1234) AND (text_field*=dog OR page_field=4321)
Link to comment
Share on other sites

Problem solved  - I just needed to give a name to the OR-group:

$pages->find("foo=(text_field*=cat, page_field=1234), foo=(text_field*=dog, page_field=4321)");

Nope, that's not right. I'm going to kick myself when I get this...

Link to comment
Share on other sites

shorter. Your solution is correct.

// will find pages with text_field*=cat OR dog OR page_field=1234 OR 4321
$items = $pages->find("(text_field*=cat|dog), (page_field=1234|4321)");

// will find pages with text_field*=cat OR page_field=1234 dog AND text_field*=dog OR page_field=4321
$items = $pages->find("(text_field*=cat), (page_field=1234)"),
$items->add("(text_field*=dog), (page_field=4321)");

// similar to
$pages->find("foo=(text_field*=cat), foo=(page_field=1234), bar=(text_field*=dog), bar=(page_field=4321)");

Well explained here: https://processwire.com/api/selectors/

Edit: Correction of correction ... :)
 

Edited by kixe
  • Like 1
Link to comment
Share on other sites

That selector doesn't give quite the right results. There's no AND component to it, so it would match any page that has cat OR dog OR 1234 OR 4321.

I think the selector in my third post...

$pages->find("foo=(text_field*=cat), foo=(page_field=1234), bar=(text_field*=dog), bar=(page_field=4321)");

...might be as succinct as it gets. And nothing wrong with it, I just thought there might be something simpler I was overlooking.

It's also totally possible to use the $items->add approach you suggested initially, just a bit more involved to deal with the pagination. Found a great post from Soma that spells it out.

  • Like 1
Link to comment
Share on other sites

I'm waiting for the coffee to kick in still, so this probably is missing something but I'll throw it out there anyway... 

text_field*=cat|dog, page_field=1234|4321

Btw, for some of the solutions above you can also leave out the "foo=" part if you want too. You only need to start naming the OR expressions if there are multiple OR expressions that should be evaluated as part of separate groups. But if you need a "bar=" too, then you'd need to name them. 

Link to comment
Share on other sites

Ryan, that selector would fail to match a page where text_field is "my cat and my dog" and page_field is "5555" (or empty).

It's taken me a little while to get my head around the way OR-groups work, but my current understanding is that if you need an AND relationship between OR-groups (as I do in my example) then the OR-groups need to be named. Otherwise the effect is (or-group) OR (or-group) OR (or-group)...etc.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...