How to create a results page that filters through multiple templates by tag and category

Hi all,

Struggling with this one, currently only showing articles on the tag results page but not the projects… have gone over the code and can’t seem to find the correct combination.

I have project pages and post pages which show the tags and category near the footer. What I’m hoping to achieve is when the users clicks on the tag, or category it directs to a results page listing everything with the same tag or category.

Thanking you in advanced for your help!

Results.php Controller:

<?php
return function($site, $pages, $page) {
// fetch the basic set of pages
$articles = $site->index()->filterBy('template', 'writing', 'project');

// add the tag filter
if($tag = param('tag')) {
$articles = $articles->filterBy('tags', $tag, ',');
 }

// fetch all tags
$tags = $articles->pluck('tags', ',', false);

 // apply pagination
$articles   = $articles->paginate(10);
$pagination = $articles->pagination();

return compact('articles', 'tags', 'tag', 'pagination'); 
} 

results php page:

    <?php snippet('header') ?>
      <main class="main" role="main">
    <ul>
      <?php foreach($articles as $articles): ?>
      <li>
        <a href="<?= $articles->url() ?>">
          <?= $articles->title()->html() ?>
        </a>
      </li>
      <?php endforeach ?>
    </ul>
</main> <?php snippet('footer') ?>

This won’t work, if you want to filter by multiple templates, you have to use an array. and. check if the template is in that array ($pages->filterBy() | Kirby CMS):

$articles = $site->index()->filterBy('template', 'in', ['writing', 'project']);

Do you really want duplicate tags, if not, set the last parameter to trueinstead.

@texnixe you are amazing, this worked perfectly, thank you! :ok_hand: