# Is it possible to fetch a page by template?

**URL:** <https://forum.getkirby.com/t/is-it-possible-to-fetch-a-page-by-template/16172>\
**Category:** Questions\
**Tags:** v3\
**Created:** [November 4, 2019, 2:37pm UTC](https://forum.getkirby.com/t/is-it-possible-to-fetch-a-page-by-template/16172 "2019-11-04T14:37:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [November 4, 2019, 2:37pm UTC](https://forum.getkirby.com/t/is-it-possible-to-fetch-a-page-by-template/16172/1 "2019-11-04T14:37:57Z")

</div>

Client has a field in Contacts page, that is used to show navigation link. And we also want to show this link in footer. I can’t fetch Contacts page with that field by URL, as it can change, also has different languages.

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [November 4, 2019, 2:46pm UTC](https://forum.getkirby.com/t/is-it-possible-to-fetch-a-page-by-template/16172/2 "2019-11-04T14:46:12Z")

</div>

You can use filterby to pick up on the template. [https://getkirby.com/docs/cookbook/content/filtering#filtering-by-methods](https://getkirby.com/docs/cookbook/content/filtering#filtering-by-methods)

---

<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:** [November 4, 2019, 2:51pm UTC](https://forum.getkirby.com/t/is-it-possible-to-fetch-a-page-by-template/16172/3 "2019-11-04T14:51:29Z")

</div>

Note that you need to `findBy('intendedTemplate', 'some-template')` instead of `template` if the template doesn’t exist in the file system. If the user is allowed to change the template, however, finding by template is just as useless as finding by uid.

Also, I would use `findBy()`, not `filterBy()`

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [November 4, 2019, 2:57pm UTC](https://forum.getkirby.com/t/is-it-possible-to-fetch-a-page-by-template/16172/4 "2019-11-04T14:57:38Z")

</div>

Working fine, thank you!

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [November 4, 2019, 3:00pm UTC](https://forum.getkirby.com/t/is-it-possible-to-fetch-a-page-by-template/16172/5 "2019-11-04T15:00:40Z")

</div>

Checked with `findBy()`, gives error “Call to a member function maplink() on null”

```
  	      $contactspage = $site
  ->index()
  ->findBy('intendedTemplate', 'contact');
```

---

<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:** [November 4, 2019, 3:03pm UTC](https://forum.getkirby.com/t/is-it-possible-to-fetch-a-page-by-template/16172/6 "2019-11-04T15:03:43Z")

</div>

That’s strange, because it should work.

On a side note: Why do you search the complete index? This is not recommended if you can avoid it. And I would suspect that the page is not anywhere in the complete site tree, but usually on the first level.

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [November 4, 2019, 3:05pm UTC](https://forum.getkirby.com/t/is-it-possible-to-fetch-a-page-by-template/16172/7 "2019-11-04T15:05:15Z")

</div>

Sorry, might have other code left, will check. Yes, page is on first level, will change it.

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [November 4, 2019, 3:09pm UTC](https://forum.getkirby.com/t/is-it-possible-to-fetch-a-page-by-template/16172/8 "2019-11-04T15:09:03Z")

</div>

Yes, it’s working fine with `findBy()`

```
      $contactspage = $site
        ->children()
        ->findBy('intendedTemplate', 'contact');

```

Thanks a lot!

---

<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:** [November 4, 2019, 3:10pm UTC](https://forum.getkirby.com/t/is-it-possible-to-fetch-a-page-by-template/16172/9 "2019-11-04T15:10:42Z")

</div>

The difference between the two methods `filterBy()` and `findBy()` is that the first returns a Pages object, the second a single Page object. If you use `filterBy()` you have to get the first element (using `first()`) or loop through the collection.

---

<div class="post-metadata">

**Author:** ![Roman](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roman/32/6571_2.png) [@Roman](https://forum.getkirby.com/u/Roman)\
**Post date:** [November 4, 2019, 3:14pm UTC](https://forum.getkirby.com/t/is-it-possible-to-fetch-a-page-by-template/16172/10 "2019-11-04T15:14:33Z")

</div>

Very nice, I still used loop with `findBy()` as thought it’s needed, because there can be more than one page with same template and not the only field. But yes, with `findBy()` works without loop, thank you!
