# Organize search results

**URL:** <https://forum.getkirby.com/t/organize-search-results/17393>\
**Category:** Questions\
**Tags:** v2\
**Created:** [February 24, 2020, 9:42am UTC](https://forum.getkirby.com/t/organize-search-results/17393 "2020-02-24T09:42:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![possible](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@possible](https://forum.getkirby.com/u/possible)\
**Post date:** [February 24, 2020, 9:42am UTC](https://forum.getkirby.com/t/organize-search-results/17393/1 "2020-02-24T09:42:13Z")

</div>

Hi there,  
i hope some people are still working with Kirby 2 around here 🙂

I was implementing the search function into the header of a website (global search) and (apart from not being able to get the query into the url) everything worked fine.  
But then i tried to organize the search results into the section of the website and stumbled upon a problem:

I can group the results with a function:

```
<?php
  $callback = function($p) {
  return $p->parents()->last();
};
$groupedItems = $results->group($callback);
?>

```

But as soon as the results include top-level pages, the whole thing crashed and gives me a “Invalid grouping value for key”.

If i remove the -\>last(), it works fine, but then i’ll get too many groups (because if 3rd level children).

How can i display the top-level-pages separately? Probably it would be ideal to show them as its own group or just ungrouped as they are…

---

<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:** [February 24, 2020, 10:59am UTC](https://forum.getkirby.com/t/organize-search-results/17393/2 "2020-02-24T10:59:00Z")

</div>

You could use an if statement

```auto
  $callback = function($p) {
    if ($p->depth() > 1) {
      return $p->parents()->last();
    } else {
      return $p;
    }
  
};

```

---

<div class="post-metadata">

**Author:** ![possible](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@possible](https://forum.getkirby.com/u/possible)\
**Post date:** [February 24, 2020, 12:26pm UTC](https://forum.getkirby.com/t/organize-search-results/17393/3 "2020-02-24T12:26:42Z")

</div>

Thanks a lot, i ended up doing exactly this. Now i can filter for the top-level and show them in a separate block.  
One more question, if i may: the search sits right now in the header of the site.  
It works fine with this:

```
<form id="searchform" action="<?php echo $pages->findBy('intendedTemplate', 'search')->url() ?>" method="post">

```

But i can’t get the resulting search page to display this in the url: ?q=searchterm

I know this has to do with the controller, which only controls the /search page, but i really have no clue how to get the logic into the header…

---

<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:** [February 24, 2020, 12:52pm UTC](https://forum.getkirby.com/t/organize-search-results/17393/4 "2020-02-24T12:52:06Z")

</div>

If you use `GET` instead of `POST`, you will see the query param in the URL. POST request is not useful for search, I think.

---

<div class="post-metadata">

**Author:** ![possible](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@possible](https://forum.getkirby.com/u/possible)\
**Post date:** [February 24, 2020, 1:03pm UTC](https://forum.getkirby.com/t/organize-search-results/17393/5 "2020-02-24T13:03:20Z")

</div>

perfect. thanks a 100 times…

---

<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:23pm UTC](https://forum.getkirby.com/t/organize-search-results/17393/6 "2025-02-06T17:23:06Z")

</div>


