Is it possible to group the search results by their Parents?
- Some News Title 123
- Some FAQ 123
- Another News Title 321
What I would like:
NEWS
- Some News Title 123
- Another News Title 321
FAQ
- Some FAQ 123
So basicly as it is described here http://getkirby.com/docs/cheatsheet/pages/groupBy but instead of grouping by year grouping by parent.
$pages->groupBy()
only works for content fields, not virtual fields like the parent()
method.
Right now you would have to iterate through all results and add the pages to an array depending on their parent like so:
$results = $something->search(...);
$groupedResults = array();
foreach($results as $result): {
if(!isset($groupedResults[$result->parent()->uri()]) $groupedResults[$result->parent()->uri()] = array();
$groupedResults[$result->parent()->uri()][] = $result;
}
And in your template:
<?php foreach($groupedResults as $group => $items): ?>
<h3><?php echo page($group)->title() ?></h3>
<?php foreach($items as $item): ?>
...
<?php endforeach ?>
<?php endforeach ?>
It looks like there will be group by callback in Kirby 2.3 or one of the next versions, which will make this more elegant.