Is it possible to use filterBy for two differents categories filters?

Hello everyone, I’m a student in Web Development, so I’m really debutant. My first teacher of web talked to me about Kirby 3 years ago, and I decided to try it now . I watched the playlist “Kirby Basics” and made the same website as videos without any problem

Now I’m trying to do an other project, and I’m trying to follow the method of “How to create a filter menu”, but for two different “categories”. (in the video he used only one)

So, my question is : Can I use the method “filterBy” for two different filters ?
This what I did : (sorry I’m beginner in development…)

Here is the 2 categories for each project: ‘source_date’ and ‘my_date’

Screenshot from 2021-04-28 20-36-01

Each categories had a type select and 4 options each.

Here is my documents.php (sorry I didn’t found how to make a better shape to my code in this post)


//création d'une variable, le paramètre get helper de kirby permet de récupérer whatever query parameter(url) and asign to the variable

$filterBy = get('filter');

$documents = $page
    ->filterBy('source_date', $filterBy)
    ->filterBy('my_date', 'novembre 2019')

$pagination = $documents->pagination();

    <h1> <?= $page->title() ?></h1>
        <?php foreach ($documents as $document) : ?>
                <a href="<?= $document->url() ?>">
                        <?= $document->image()->crop(400) ?>

                        <figcaption><?= $document->title() ?></figcaption>

                Date source : <small><?= $document->source_date() ?></small> <br>
                Date découverte : <small><?= $document->my_date() ?></small>
        <?php endforeach ?>

So with that, when I enter in the url http://localhost:8082/documents?filter=en%202010, it works I there is only document who had ‘my_date’=‘novembre 2019’ and ‘source_date’=’$filterBy’.
So now I want to add $filterBy to ‘my_date’ for when I’ll enter in the url http://localhost:8082/documents?filter=en%202010&?filter=2010 it display only document ‘my_date’=novembre 2019 and ‘source_date’=2010.

$filterBy = get('filter');

$documents = $page
    ->filterBy('source_date', $filterBy)
    ->filterBy('my_date', $filterBy)

But first : it don’t works, and second I’m not 100% sure it’s possible do that in this way…
Can you show me the right way ?

Ok, so sorry for my approximativ english, I hope you’ll understand me
Thank You ! :slightly_smiling_face:

Hey Clara,

yes, it is possible to filter by as many filters as you want, only you should not use the same filter name/variable. So if we stick with query strings, your URL could for example look like this:


You would then have two variables

$dateFilter   = get('datefilter');
$sourceFilter = get('sourcefilter');

Where the names are totally up to you.

The easiest way to apply these filters conditionally would be using the when() method

$documents = $page->children()->listed()
    ->when($dateFilter, function ($dateFilter) {
        return $this->filterBy('my_date', $dateFilter);
    ->when($sourceFilter, function ($sourceFilter) {
        return $this->filterBy('source_date', $sourceFilter);

1 Like

Thank you a lot texnixe, it helped me a lot !!
Thanks a lot for your rapidity ! :slight_smile: