# Kirby 2.3 group(callback) -\> function

**URL:** <https://forum.getkirby.com/t/kirby-2-3-group-callback-function/4647>\
**Category:** Questions\
**Tags:** v2\
**Created:** [July 8, 2016, 2:26pm UTC](https://forum.getkirby.com/t/kirby-2-3-group-callback-function/4647 "2016-07-08T14:26:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![possible\_books](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/possible_books/32/1707_2.png) [@possible\_books](https://forum.getkirby.com/u/possible_books)\
**Post date:** [July 8, 2016, 2:26pm UTC](https://forum.getkirby.com/t/kirby-2-3-group-callback-function/4647/1 "2016-07-08T14:26:48Z")

</div>

Hi there,  
did anybody already experimented with the new group-function?  
I’d like to group projects by category, which can be more than one per project (like tags).

There are no examples for this in [https://getkirby.com/docs/toolkit/api/collection/group](https://getkirby.com/docs/toolkit/api/collection/group)

So, i would like to do something like this:

```
$category = $works->children()->visible()->group(function($categories) {
	return ( group by $value, separated by ',' sort asc)
	}); 

```

is that possible? and as we are at it: is it possible to use a global value for the group by value?  
For example: when you have a param (year or category) in the url this could be used within the grouping function. Like: $page-\>$param because $page-\>param() would search for a textfield called param:

Thanks (as always)  
Matthias

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [July 8, 2016, 2:37pm UTC](https://forum.getkirby.com/t/kirby-2-3-group-callback-function/4647/2 "2016-07-08T14:37:23Z")

</div>

Each item can only have one group, so you currently can’t put each project into all of their respective categories, only in one.

What you could do is to `pluck()` all possible category values, iterate through them and then filter the matching projects:

```auto
<?php foreach($works->children()->visible()->pluck($value) as $category): ?>
  <h2><?= $category ?></h2>
  <?php foreach($works->children()->visible()->filterBy($value, $category, ',') as $project): ?>
    ...
  <?php endforeach ?>
<?php endforeach ?>

```

**Warning** : Please don’t allow `$value` to be set by the visitor (in the URL) without validation. Otherwise users could call the writing methods like `sort()` or `hide()`. Only allow `$value`s from a whitelist.

---

<div class="post-metadata">

**Author:** ![possible\_books](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/possible_books/32/1707_2.png) [@possible\_books](https://forum.getkirby.com/u/possible_books)\
**Post date:** [July 9, 2016, 9:04am UTC](https://forum.getkirby.com/t/kirby-2-3-group-callback-function/4647/3 "2016-07-09T09:04:25Z")

</div>

Hi Lukas and thanks,  
i thought that the group() function could be able to have one item in several groups.  
I managed to find a workaround with pluck() as you suggested, but i am struggling with the injection of a value:

```
<?php 
$param = param('by'); 
$values = $page->children()->pluck($param, ',', true);
?>

```

$param is in this case “year” or “category”  
… after the first iteration through the $values

```
<?php $catworks = $works->filter(function($child) use ($value) {
	return in_array($value, $child->$param->split());
	}); 
?>

```

I will use a whitelist later on, but for now i can’t figure out how to use a previously defined value ($param) to create something like: $child-\>year()-\>split()…

Matthias

---

<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 9, 2016, 9:16am UTC](https://forum.getkirby.com/t/kirby-2-3-group-callback-function/4647/4 "2016-07-09T09:16:32Z")

</div>

You have to use the variable as a method and $param is not known in your filter callback, so you need to pass that to `use()` as well:

```auto
<?php $catworks = $works->filter(function($child) use ($value, $param) {
	return in_array($value, $child->$param()->split());
	}); 
?>

```

---

<div class="post-metadata">

**Author:** ![possible\_books](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/possible_books/32/1707_2.png) [@possible\_books](https://forum.getkirby.com/u/possible_books)\
**Post date:** [July 9, 2016, 9:52am UTC](https://forum.getkirby.com/t/kirby-2-3-group-callback-function/4647/5 "2016-07-09T09:52:27Z")

</div>

Nice!  
Thanks a lot. That works. I didn’t get the declaration part…

---

<div class="post-metadata">

**Author:** ![lukasbestle](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/lukasbestle/32/11437_2.png) [@lukasbestle](https://forum.getkirby.com/u/lukasbestle)\
**Post date:** [July 9, 2016, 10:37am UTC](https://forum.getkirby.com/t/kirby-2-3-group-callback-function/4647/6 "2016-07-09T10:37:35Z")

</div>

As I wrote above, make sure to validate the param though, otherwise you will have a security issue!

---

<div class="post-metadata">

**Author:** ![possible\_books](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/possible_books/32/1707_2.png) [@possible\_books](https://forum.getkirby.com/u/possible_books)\
**Post date:** [July 9, 2016, 11:10am UTC](https://forum.getkirby.com/t/kirby-2-3-group-callback-function/4647/7 "2016-07-09T11:10:15Z")

</div>

done & dusted.  
i have a whitelist of values that i check against, so all other values will returned to a default page.

thanks again

---

<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, 4:57pm UTC](https://forum.getkirby.com/t/kirby-2-3-group-callback-function/4647/8 "2025-02-06T16:57:33Z")

</div>

Archived on February 6, 2025, 4:57pm UTC


