# How to exclude pages in Site wide Search

**URL:** <https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092>\
**Category:** Questions\
**Tags:** v3\
**Created:** [July 28, 2022, 9:10pm UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092 "2022-07-28T21:10:50Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsim412](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@dsim412](https://forum.getkirby.com/u/dsim412)\
**Post date:** [July 28, 2022, 9:10pm UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/1 "2022-07-28T21:10:50Z")

</div>

I have a simple site-wide search page. In my search.php controller, I’m attempting to have the search NOT include a page (‘members’) and its children pages. I tried ‘not’ method and ‘not in’ but it still searches all of the pages.

This code does NOT work:

> $results = $site-\>search( $query, ‘title|text|blocks’ )-\>listed()-\>not([page( ‘members’ )]);

I also tried this:

> $results = $site-\>search( $query, ‘title|text|blocks’ )-\>listed()-\>filterBy( ‘pages’, ‘not in’, [‘members’]);

I know I can filter by templates and tags but is there a way to filter by pages or exclude certain pages when doing a site-wide search?

---

<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:** [July 28, 2022, 9:22pm UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/2 "2022-07-28T21:22:07Z")

</div>

```auto
$excluded = page('members')->children();
$excluded = $excluded->add(page('members');
$results = $site->search( $query, ‘title|text|blocks’ )->listed()->not( $excluded);

```

Although it would probably make more sense to filter the page to search first, not the results?

---

<div class="post-metadata">

**Author:** ![dsim412](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@dsim412](https://forum.getkirby.com/u/dsim412)\
**Post date:** [July 29, 2022, 3:40pm UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/3 "2022-07-29T15:40:29Z")

</div>

Thank you for your quick response. I had to add the **listed()** method to be able to use the **add()** method. The page wouldn’t display even in debug mode. Not sure why that is because both the children() and listed() return the same type.

```auto
$excluded = page( 'members' )->children()->listed();
$excluded->add(page('members'));
    
$results = $site->search( $query, 'title|text|blocks' )->listed()->not( $excluded );

```

The above code works for the main page and its children, but it doesn’t exclude any grandchildren or any deeper subpages.

So I modified the code to use the **index()** method. That works. However, I know the index() method can be pretty heavy since it’s looking through the entire contents including files. Is there another way besides using that method to exclude the parent ‘members’ page and all of it’s subpages?

```auto
$excluded = page( 'members' )->index()->listed();

$results = $site->search( $query, 'title|text|blocks' )->listed()->not( $excluded );

```

---

<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:** [July 29, 2022, 3:44pm UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/4 "2022-07-29T15:44:53Z")

</div>

> [@dsim412](#):
>
> Is there another way besides using that method to exclude the parent ‘members’ page and all of it’s subpages?

The warning basically refers to using `$site->index()` in large installations. Of course, if you your members page also has thousands of subpages in the tree, you might run into the same issue. I hope, that is not the case, otherwise, you will have to cache the collection.

---

<div class="post-metadata">

**Author:** ![roland](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roland/32/6288_2.png) [@roland](https://forum.getkirby.com/u/roland)\
**Post date:** [March 10, 2023, 4:26pm UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/5 "2023-03-10T16:26:48Z")

</div>

Hi @texnixe and @dsim412,

can you tell me, how it is possible to include only published pages in search?

Where do i have to put the code in the search.php controller?

Thank you very much,

Roland.

---

<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:** [March 10, 2023, 4:30pm UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/6 "2023-03-10T16:30:51Z")

</div>

> [@roland](#):
>
> to include only published pages

A published page is a page that is either listed or unlisted and since drafts are not publicly accessible anyway, there is nothing special to do. Unless you mean only listed pages.

---

<div class="post-metadata">

**Author:** ![roland](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roland/32/6288_2.png) [@roland](https://forum.getkirby.com/u/roland)\
**Post date:** [March 10, 2023, 4:51pm UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/7 "2023-03-10T16:51:32Z")

</div>

ah yes, i ment only listed pages. sorry.

the result should be, that unlisted pages are not shown.

---

<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:** [March 10, 2023, 4:59pm UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/8 "2023-03-10T16:59:21Z")

</div>

And what’s your current search code?

---

<div class="post-metadata">

**Author:** ![roland](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roland/32/6288_2.png) [@roland](https://forum.getkirby.com/u/roland)\
**Post date:** [March 11, 2023, 10:25am UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/9 "2023-03-11T10:25:27Z")

</div>

what do you mean with search code?  
when i go to the website “[www.mywebsite.de](http://www.mywebsite.de)  
/search?q=abc” i only want listed sites with “abc” shown.  
is this, what you mean?

---

<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:** [March 11, 2023, 10:37am UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/10 "2023-03-11T10:37:18Z")

</div>

No, I mean the code that is responsible for showing the search result, probably a controller `/site/controllers/search.php`. You did not develop the site then?

---

<div class="post-metadata">

**Author:** ![roland](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/roland/32/6288_2.png) [@roland](https://forum.getkirby.com/u/roland)\
**Post date:** [March 11, 2023, 1:46pm UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/11 "2023-03-11T13:46:42Z")

</div>

i did not develop. i use the awesome zero one plugin. But you give the right hint. In the `/site/controllers/search.php` i changed

$results = $site-\>index()-\>published()-\>bettersearch($query);

into

$results = $site-\>index()-\>listed()-\>bettersearch($query);

And this seems to work!!!

Thank you so much. You are awesome, too!!!

---

<div class="post-metadata">

**Author:** ![GB\_DESIGN](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gb_design/32/14956_2.png) [@GB\_DESIGN](https://forum.getkirby.com/u/GB_DESIGN)\
**Post date:** [December 27, 2023, 8:26am UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/12 "2023-12-27T08:26:51Z")

</div>

> [@texnixe](#):
>
> ```auto
> $excluded = page('members')->children();
> $excluded = $excluded->add(page('members'));
> $results = $site->search( $query, ‘title|text|blocks’ )->listed()->not( $excluded);
> 
> ```

Has anything changed with Kirby 4?  
I get an error message, if I extend the code in the controller:

> Error thrown with message “Call to a member function children() on null”

Unfortunately, I could not find a working solution here in the forum to exclude several pages from the search. I also can’t find a description in the cookbook for the search function either.

---

<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:** [December 27, 2023, 9:51am UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/13 "2023-12-27T09:51:38Z")

</div>

Please tell me in which line in your code the error appears and what you have on that line.

The error message means that you are calling the `children()` method on a non-existing page object (null).

---

<div class="post-metadata">

**Author:** ![GB\_DESIGN](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gb_design/32/14956_2.png) [@GB\_DESIGN](https://forum.getkirby.com/u/GB_DESIGN)\
**Post date:** [December 27, 2023, 10:00am UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/14 "2023-12-27T10:00:33Z")

</div>

Thanks for your quick help.  
This is the complete code:

**`/site/controllers/search.php`**

```auto
return function ($site) {

  $query = get('q');
  
  $excluded = page('aktionen')->children();
	$excluded = $excluded->add(page('aktionen'));
	$results = $site->search( $query, 'title|headline|subheadline|text|layout')->listed()->not($excluded);

  return [
    'query' => $query,
    'results' => $results,
  ];

};

```

This line triggers the error message:  
**`$excluded = page('aktionen')->children();`**

The “aktionen” page contains subpages, so I don’t understand the error message.

---

<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:** [December 27, 2023, 10:02am UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/15 "2023-12-27T10:02:25Z")

</div>

> [@GB\_DESIGN](#):
>
> page(‘aktionen’)

Does this page really exist? With the slug `aktionen`, and as a folder directly in `/content`, not as child of another page?

---

<div class="post-metadata">

**Author:** ![GB\_DESIGN](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gb_design/32/14956_2.png) [@GB\_DESIGN](https://forum.getkirby.com/u/GB_DESIGN)\
**Post date:** [December 27, 2023, 10:14am UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/16 "2023-12-27T10:14:45Z")

</div>

You have already given me the solution: I used the name of the blueprint/template, not the slug. Now the subpages no longer appear in the search. 👏

---

<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:** [December 27, 2023, 10:26am UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/17 "2023-12-27T10:26:46Z")

</div>

You might want to consider using the UUID as a parameter for the `page()` helper instead of the slug, so that if the page is later renamed, it will still be found.

---

<div class="post-metadata">

**Author:** ![GB\_DESIGN](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/gb_design/32/14956_2.png) [@GB\_DESIGN](https://forum.getkirby.com/u/GB_DESIGN)\
**Post date:** [December 27, 2023, 1:39pm UTC](https://forum.getkirby.com/t/how-to-exclude-pages-in-site-wide-search/26092/18 "2023-12-27T13:39:08Z")

</div>

Perfect! I’ll keep your tip in mind. In my case, this link will never change. But there will certainly be a situation where the UUID makes more sense instead of the slug.
