# Filter pages by page field's content

**URL:** <https://forum.getkirby.com/t/filter-pages-by-page-fields-content/26442>\
**Category:** Questions\
**Tags:** v3\
**Created:** [September 8, 2022, 2:46pm UTC](https://forum.getkirby.com/t/filter-pages-by-page-fields-content/26442 "2022-09-08T14:46:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamiehunter](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jamiehunter](https://forum.getkirby.com/u/jamiehunter)\
**Post date:** [September 8, 2022, 2:46pm UTC](https://forum.getkirby.com/t/filter-pages-by-page-fields-content/26442/1 "2022-09-08T14:46:13Z")

</div>

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:

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

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

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [September 8, 2022, 3:48pm UTC](https://forum.getkirby.com/t/filter-pages-by-page-fields-content/26442/2 "2022-09-08T15:48:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jamiehunter](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jamiehunter](https://forum.getkirby.com/u/jamiehunter)\
**Post date:** [September 8, 2022, 3:55pm UTC](https://forum.getkirby.com/t/filter-pages-by-page-fields-content/26442/3 "2022-09-08T15:55:36Z")

</div>

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();
});

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [September 8, 2022, 3:59pm UTC](https://forum.getkirby.com/t/filter-pages-by-page-fields-content/26442/4 "2022-09-08T15:59:58Z")

</div>

Try

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

```

---

<div class="post-metadata">

**Author:** ![jamiehunter](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jamiehunter](https://forum.getkirby.com/u/jamiehunter)\
**Post date:** [September 8, 2022, 4:05pm UTC](https://forum.getkirby.com/t/filter-pages-by-page-fields-content/26442/5 "2022-09-08T16:05:48Z")

</div>

That fixed it, thank you!
