Jump to content

Correct syntax for OR find by path


Robin S
 Share

Recommended Posts

The API docs have an example for OR selectors, to match any of a group of page IDs.

id=123|124|125

What is the correct syntax for doing something similar but using page paths? I tried...

$pages->find("path=/news/|/about/")

...but no success.

Link to comment
Share on other sites

This should do the trick:

$pages->find("(path=/news/),(path=/about/)");

And here is some more info on "OR group" selectors: https://processwire.com/talk/topic/3768-processwire-dev-branch/?p=64049

Or, depending on your needs, you could maybe match by name rather than path:

$pages->find("name=news|about");

Of course there is the chance that another page with name "news" or "about" may get created elsewhere in the page tree, so you could help mitigate this by supplying a parent of home as well:

$pages->find("parent=/, name=news|about");

I am curious though what is wrong with using the ID - seems like it might be the safest option? Just be sure to add a comment above the selector so you know what they are referring to.

  • Like 2
Link to comment
Share on other sites

This should do the trick:

$pages->find("(path=/news/),(path=/about/)");

Thanks adrian, that works fine, although it does start to get a bit long-winded depending on how many pages you want to match. Any idea why the pipe syntax can't be used like with ID and other fields?

In my case I'm selecting pages to appear in the main navigation. I was using name but then an editor created a news item called "News"... :rolleyes: 

So was aiming for something more unique than name but more readable than ID. The combination of name and parent is a great suggestion.

Link to comment
Share on other sites

Any idea why the pipe syntax can't be used like with ID and other fields?

I haven't investigated very far, but I am seeing an array vs string error due to: https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/core/PageFinder.php#L1182

There is a note in that file that I think might be related: 

// TODO Make native fields and path/url multi-field and multi-value aware

So it is possible that support is planned but hasn't been implemented yet.

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...