Find a page which has current page selected in a pages field / Cross-referencing pages

Hi,
I’m struggling to figure this out:

I have a page (“directory-one/page-one”) which has a pages select field in the panel. In this case, the selected page would be “directory-two/page-two”.

When I visit “Page Two” in the frontend, I want to create a cross-reference to the page which has this page selected.

I was trying to approach this with a filter function like this:

// on "Page Two"
$wantedPage = page('directory-one')
->children()
->listed()
->filterBy('selectedPage', $page->id())

I thought $page->id() to be the same string the one that would appear on the content file of “Page One”, but on Page One it contains:

SelectedPage:

- >
  directory-two/page-two

Is it possible to solve this without doing something like ->filterBy('selectedPage', '- > ', $page->id())?

Thanks in advance!

There will be a collection filter attribute in the next Kirby version, but currently, you have to do it like this:

$wantedPage = page('directory-one')
->children()
->listed()
->filter(function($child) use($page) {
  return $child->selectedPage()->toPage() === $page;
})->first();
1 Like

Thank you!