Robin S Posted December 13, 2015 Posted December 13, 2015 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)
Robin S Posted December 13, 2015 Author Posted December 13, 2015 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...
Robin S Posted December 13, 2015 Author Posted December 13, 2015 $pages->find("foo=(text_field*=cat), foo=(page_field=1234), bar=(text_field*=dog), bar=(page_field=4321)"); Is that right? Anything more succinct that that possible?
kixe Posted December 13, 2015 Posted December 13, 2015 (edited) 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 December 13, 2015 by kixe 1
Robin S Posted December 13, 2015 Author Posted December 13, 2015 Thanks. I considered the $items->add approach but it makes sorting and pagination more difficult than with a single find operation. 1
kixe Posted December 13, 2015 Posted December 13, 2015 I edited my post while you was answering. $items->add is not needed here. Have a look at the edit.
Robin S Posted December 13, 2015 Author Posted December 13, 2015 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. 1
ryan Posted December 13, 2015 Posted December 13, 2015 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.
Robin S Posted December 13, 2015 Author Posted December 13, 2015 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now