How to get all unique inputs from a (number) field

Hi,

I am working on a site which has projects from different years. I thought it would be easiest to just create a number field for the year and then sort the projects by using the field in the query.

What I want to do now is get all unique inputs from this field. So if in the panel someone created a project and filled the year field with 2020 and another with 2021, I want to get 2020 and 2021 as an array. Is that possible with a query?

You can use pluck(): $pages->pluck() | Kirby CMS

1 Like

Thank you! That was exactly the function I was looking for but I probably didn’t know how to search for it.

I used pluck() in combination with array_unique() to get exactly what I wanted.

Pluck has a third parameter ($unique) that you can set to true to get only unique elements, which internally uses array_unique() but less code to write.

$pages->pluck('somefield', ',', true);
1 Like