Filtering pages with multiple tags at the same time


I created 2 different filters in my controller for my products page.


return function($site, $pages, $page) {

 $perpage  = $page->perpage()->int();

  $articles = $page->children()->visible()->flip();
  $allarticles = $pages->findByURI('products')->children()->visible()->flip();

  if($tag = param('tag')) {
    $articles = $articles->filterBy('tags', $tag, ',');

  if($subtag = param('subtag')) {
    $articles = $articles->filterBy('subtags', $subtag, ',');

  $tags = $articles->pluck('tags', ',', true);
  $alltags = $allarticles->pluck('tags', ',', true);

  $subtags = $articles->pluck('subtags', ',', true);
  $allsubtags = $allarticles->pluck('subtags', ',', true);

  $articles   = $articles->paginate(($perpage >= 1)? $perpage : 5);
  $pagination = $articles->pagination();

  return compact('articles', 'tags', 'alltags', 'tag', 'subtags', 'allsubtags', 'subtag', 'pagination');


In my products page there are two loops with all the tags and the subtags

<?php $p = kirby()->request()->params()->tag(); ?>
<?php foreach($alltags as $tag): ?>
<li class="<?php ecco($tag == $p, 'active-tag') ?> ">
  <a href="<?php echo url($page->url() . '/' . url::paramsToString(['tag' => $tag])) ?>">
    <?php echo html($tag) ?>
<?php endforeach ?>

<?php $p = kirby()->request()->params()->subtag(); ?>
<?php foreach($allsubtags as $subtag): ?>
<li class="<?php ecco($subtag == $p, 'active-tag') ?> ">
  <a href="<?php echo url($page->url() . '/' . url::paramsToString(['subtag' => $subtag])) ?>">
    <?php echo html($subtag) ?>
<?php endforeach ?>

Everything works perfectly, but I can filter only one element at a time. My dream is to filter one element from the tag (first filter) and one element from the subtag (second filter) at the same time (show the the sum of the two filters).

Is it possible?
Can you help me?

It’s possible but not with your current setup. Your URLs only ever deliver one tag at a time. For your use case, rather than using URLs, a form. with selects for tags and sub tags would be more suitable.

Ok, thanks. There is an example of this solution? I don’t know how to start. :sweat:

Maybe the filtering compendium is a starting point.