Find page by slug

Hello. I thought I had this one without much fuss, but I’m missing something. I’m trying to find a page by its slug.

$pg = $site->search('5fbd9b027279f');

No? No.

If the page can be anywhere in the tree:

if ( $pg = $site->index()->findBy('slug', '5fbd9b027279f') ) {
  echo $pg->title();

search() searches in the page content, and the slug is not part of the content.

Yep. That works. Thanks!

Any tips for making more performant? The page I’m searching for will use a specific template. It’s grandparent (hmm) will be the same.

Searching the complete index can indeed be a performance hit, especially when you have a many pages.

Don’t you know the id of the page, so you could use the page helper?


The parent of the page I’m searching for is unknown - it could be different.

Not even the main upper level page?

for example:

page('main-page')->index()->findBy('slug', '5fbd9b027279f');

The more you can reduce this, the better.

I know the grandparent. So yes. That should work.

And yep. That works. Excellent! That’s probably the best we can do. Overall there will not be a ton of pages to sift though.