# Filter child pages by template not working

**URL:** <https://forum.getkirby.com/t/filter-child-pages-by-template-not-working/6840>\
**Category:** Questions\
**Tags:** v2\
**Created:** [March 3, 2017, 6:10pm UTC](https://forum.getkirby.com/t/filter-child-pages-by-template-not-working/6840 "2017-03-03T18:10:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AhoyLemon](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/ahoylemon/32/4762_2.png) [@AhoyLemon](https://forum.getkirby.com/u/AhoyLemon)\
**Post date:** [March 3, 2017, 6:10pm UTC](https://forum.getkirby.com/t/filter-child-pages-by-template-not-working/6840/1 "2017-03-03T18:10:49Z")

</div>

Hey folks,

Running into an issue right now where I’m trying to do two different foreach on child pages, depending on which template that page is using, however, for some reason, that’s not working.

Here’s what I got…

```auto
<?php foreach($page->children()->filterBy('template', 'act_ballroom') as $act) { ?>
<h1>FOUND ONE</h1>
<?php } ?>

```

This solution doesn’t bring up anything. However, if I try…

```auto
<?php foreach($page->children() as $act) { ?>
<h1>FOUND ONE</h1>
<?php } ?>

```

…I do get a hit. I’m 100% certain the page has a template of `act_ballroom`, so I’m not sure where I’m going wrong here.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![AhoyLemon](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/ahoylemon/32/4762_2.png) [@AhoyLemon](https://forum.getkirby.com/u/AhoyLemon)\
**Post date:** [March 3, 2017, 6:16pm UTC](https://forum.getkirby.com/t/filter-child-pages-by-template-not-working/6840/2 "2017-03-03T18:16:41Z")

</div>

And **immediately** after posting that I figured out the solution. I’ll write it here in case Googling led you here.

the `filterBy('template)` function is defined by what _template_ the page will end up as, and _not_ what kind of blueprint you used to make it. So, if you have the content file as `act_ballroom.txt`, but you haven’t yet build a `act_ballroom.php` template in your templates folder, this will come through as `default` and therefore your filter won’t work as intended.

**TLDR:** Make sure you have a template matching your blueprint to use filterBy(template)

---

<div class="post-metadata">

**Author:** ![daybugging](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@daybugging](https://forum.getkirby.com/u/daybugging)\
**Post date:** [March 3, 2017, 6:19pm UTC](https://forum.getkirby.com/t/filter-child-pages-by-template-not-working/6840/3 "2017-03-03T18:19:05Z")

</div>

What about:

```php
<?php foreach($page->children() as $act) {
if($act->template() == 'act_ballroom') { ?>
<h1>FOUND ONE</h1>
<?php } else { ?>
// do something else
<?php } ?>

```

---

<div class="post-metadata">

**Author:** ![pedroborges](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/pedroborges/32/7038_2.png) [@pedroborges](https://forum.getkirby.com/u/pedroborges)\
**Post date:** [March 3, 2017, 7:24pm UTC](https://forum.getkirby.com/t/filter-child-pages-by-template-not-working/6840/4 "2017-03-03T19:24:24Z")

</div>

> [@AhoyLemon](#):
>
> So, if you have the content file as act\_ballroom.txt, but you haven’t yet build a act\_ballroom.php template in your templates folder, this will come through as default and therefore your filter won’t work as intended.

Your finding is correct. However, any Page also have the `intendedTemplate()` method to cover cases like yours. So this should work:

```php
<?php foreach($page->children()->filterBy('intendedTemplate', 'act_ballroom') as $act) : ?>
    <h1><?php echo $act->title()->html() ?></h1>
<?php endforeach ?>

```

---

<div class="post-metadata">

**Author:** ![AhoyLemon](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/ahoylemon/32/4762_2.png) [@AhoyLemon](https://forum.getkirby.com/u/AhoyLemon)\
**Post date:** [March 3, 2017, 7:39pm UTC](https://forum.getkirby.com/t/filter-child-pages-by-template-not-working/6840/5 "2017-03-03T19:39:31Z")

</div>

Oooh! That’s great to learn. Thanks, @pedroborges

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:52pm UTC](https://forum.getkirby.com/t/filter-child-pages-by-template-not-working/6840/6 "2025-02-06T16:52:30Z")

</div>

Archived on February 6, 2025, 4:52pm UTC


