Organize search results

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

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…

You could use an if statement

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

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…

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.

perfect. thanks a 100 times…