How to filter by multiple parameters?

I have blocks of subpages that are filtered by category I am then on top of that trying to further filter by subject type when subject type link is clicked

How can I filter my subpages by both these parameters?

Nav:

<?php $projects = $page->children()->listed(); ?>
<?php $tags = $projects->pluck('subject', ',', true); ?>
<?php foreach($tags as $tag): ?>
    <a href="/page/subject::<?= $tag ?>">
        <?= $tag ?>
    </a>
<?php endforeach ?>

Subpages:

<?php $filterColours = $page->children()->listed(); ?>
<?php $coloursArray = $filterColours->pluck('colours'); ?>
<?php $colours = array_unique($coloursArray)?>

<?php if( param('subject') == ''): ?>
    <?php foreach ($colours as $colour):?>
    <div class="colour-wrapper">
        <?php foreach ($page->children()->listed()->filterBy('colours', $colour, ',') as $subpage): ?>

            // Subpages filtered into colour blocks

        <?php endforeach ?> 
        </div>
    <?php endforeach ?> 
<?php else:?>
<?php if ($tag = urldecode(param('subject'))); ?>

<?php foreach ($colours as $colour):?>
    <div class="colour-wrapper">
        <?php foreach ($page->children()->listed()->filterBy('colours', $colour, 'subject', $tag, ',') as $subpage): ?>

             // Subpages filtered into colour blocks further filtered by subject type

        <?php endforeach ?> 
        </div>
    <?php endforeach ?> 

<?php endif ?>

Does each page have a single color or can there be multiple colors per page?

In any case, your code can be simplified like this:

$articles = $page->children()->listed(); ?>
if ($tag = urldecode(param('subject')) ) {
  $articles = $articles->filterBy('subject', $tag, ',');
}
$colours = $articles->pluck('colours', ',', true);
?>
<?php foreach ($colours as $colour):?>
<div class="colour-wrapper">
    <?php foreach ($articles->filterBy('colours', $colour, ',') as $subpage): ?>

        <?php echo $subpage->title() ?>

    <?php endforeach ?> 
    </div>
<?php endforeach ?> 
1 Like

This is exactly what I was looking for :slight_smile:

One issue though - the subpages aren’t coming through. With the code below $colour dumps the colour value and $articles dumps the array of subpages but $subpage is showing nothing

<?=dump($colour)?>
<?=dump($articles)?>

<?php foreach ($articles->filterBy('colours', $colour, ',') as $subpage): ?>
<?=dump($subpage)?>
<?php endforeach ?>

Then the articles collection filtered by colour seems to be an empty collection. But it should at least return something for one of the colours.

Please check what $filtered in this example returns:

<?php foreach ($colours as $colour):?>
<div class="colour-wrapper">
    <?php 
     $filtered = $articles->filterBy('colours', $colour, ',');
     dump($filtered);
     foreach ( $filtered as $subpage ) :

?>

        <?php echo $subpage->title() ?>

    <?php endforeach ?> 
    </div>
<?php endforeach ?> 
1 Like

You’re the best <3