Filter pages by page field's content

I’m trying to filter related pages where the relationship is based on the content of a page selected in a page field.

The context is that I have a Review page which has a Place page linked via a Pages Field. The Place page has a structure field of locations which contains Districts. I’d like to return Reviews of places which have the same District (first matching is fine).

I can’t find a similar example in the docs or Filtering Compendium, so here’s my code (that’s not returning anything) which might help understand what I’m trying to do:

$place = $page->place()->toPage();
$district = $place->locations()->toStructure()->first()->district();

$moreDistrict = $kirby->collection('reviews')->filter(function ($child) use ($district){
        return $child->place()->toPage()->locations()->toStructure()->first()->district() === $district;

Hm, in theory your code looks ok.

But in which page are we here? How is the reviews collection defined?

Could you also outline your page structure?

That code is in the controller for the review template. I want to show “More Reviews from <?= $district ?>” at the bottom of that page, but my code is returning an empty Pages Object even though matches exist.

The collection is fine because I can return all Reviews with a valid “Place” like this:

$moreDistrict = $kirby->collection('reviews')->filter(function ($child) use ($district){
    return $child->place()->toPage();


$district = $place->locations()->toStructure()->first()->district()->value();
// and then
return $child->place()->toPage()->locations()->toStructure()->first()->district()->value() === $district;
1 Like

That fixed it, thank you!