# Search exclude pages from results

**URL:** <https://forum.getkirby.com/t/search-exclude-pages-from-results/11655>\
**Category:** Questions\
**Tags:** v2\
**Created:** [November 14, 2018, 4:12pm UTC](https://forum.getkirby.com/t/search-exclude-pages-from-results/11655 "2018-11-14T16:12:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oziris](https://avatars.discourse-cdn.com/v4/letter/o/df788c/32.png) [@Oziris](https://forum.getkirby.com/u/Oziris)\
**Post date:** [November 14, 2018, 4:12pm UTC](https://forum.getkirby.com/t/search-exclude-pages-from-results/11655/1 "2018-11-14T16:12:51Z")

</div>

Hi,

As is it mentionned in the title, I would like to make a search function, excluding multiples pages.  
I tried to make one by copying a thread I found here, but I’m not sure this is the right method : instead of ignoring some pages, I try to `filterBy` the pages where the search needs to be executed.

```
<?php

return function($site, $pages, $page) {

    $query = get('results');
   
    $exclude = $site->index()->visible()->filter(function($child) {
        return $child->hasTemplate() == 'works' or
            $child->hasTemplate() == 'bio' ;
    });

    $results = $exclude->search($query, array('words' => true));

    return array(
        'query' => $query,
        'results' => $results,
    ); 

};

```

But here, ‘Bio’ or ‘Works’ are always returned

---

<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 14, 2018, 4:42pm UTC](https://forum.getkirby.com/t/search-exclude-pages-from-results/11655/2 "2018-11-14T16:42:26Z")

</div>

Based on my own search that filters out certain pages by template, try this… amend ‘title|text’ for the names of the content fields you want to search through…

```auto
<?php
return function($site, $pages, $page) {

  // Search Results
  $query = get('results');

  // Filter the result....
  $results = $site->search($query, 'title|text')->visible()->filterBy('template', 'not in', ['works', 'bio']);

  // Pass these off to the template
  return compact('query', 'results');

};

```

---

<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 14, 2018, 4:47pm UTC](https://forum.getkirby.com/t/search-exclude-pages-from-results/11655/3 "2018-11-14T16:47:49Z")

</div>

It makes more sense to limit the pages first, then search. Otherwise, you search within pages that you don’t even have to touch. @oziris Your logic is the wrong way round, because you want to exclude those pages, not use only those pages.

---

<div class="post-metadata">

**Author:** ![Oziris](https://avatars.discourse-cdn.com/v4/letter/o/df788c/32.png) [@Oziris](https://forum.getkirby.com/u/Oziris)\
**Post date:** [November 14, 2018, 4:49pm UTC](https://forum.getkirby.com/t/search-exclude-pages-from-results/11655/4 "2018-11-14T16:49:20Z")

</div>

Houuuu very nice, thanks ! This works great, I didn’t found it but this is mentionned in [Filtering Compendium](https://getkirby.com/docs/cookbook/filtering).

---

<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 14, 2018, 4:50pm UTC](https://forum.getkirby.com/t/search-exclude-pages-from-results/11655/5 "2018-11-14T16:50:14Z")

</div>

You welcome, but i did make a small mistake… as @texnixe says, do the filter first, then search the remaining pages.

---

<div class="post-metadata">

**Author:** ![Oziris](https://avatars.discourse-cdn.com/v4/letter/o/df788c/32.png) [@Oziris](https://forum.getkirby.com/u/Oziris)\
**Post date:** [November 14, 2018, 4:52pm UTC](https://forum.getkirby.com/t/search-exclude-pages-from-results/11655/6 "2018-11-14T16:52:49Z")

</div>

Oki doki, thanks !

```
<?php

return function($site, $pages, $page) {

    $query = get('results');

    $results = $site->index()->visible()->filterBy('template', 'not in', ['works', 'bio'])->search($query, array('words' => true));

    return array(
        'query' => $query,
        'results' => $results,
    ); 

};
```

---

<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, 5:25pm UTC](https://forum.getkirby.com/t/search-exclude-pages-from-results/11655/7 "2025-02-06T17:25:51Z")

</div>

Archived on February 6, 2025, 5:25pm UTC


