Tagged posts not filtering when active

Hi guys,
So I am having issues filtering posts within my articles page.
Currently once you click on a tag it generates the correct url /tag:tagpost but doesn’t actually filter out the other posts that don’t have that tag.

I don’t know what I am doing wrong. Below is my current setup – any help with be appreciated

template file:

<?php foreach(str::split($article->tags()) as $tag): ?>
    <a  href="<?php echo url('articles/' . url::paramsToString(['tag' => $tag])) ?>">
        <?php echo html($tag) ?>
    </a>
<?php endforeach ?>

controller file:
<?php

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

  // fetch the basic set of pages
  $articles = $page->children()->visible()->flip();

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

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

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

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

};

Hm, could you please post your complete template? Are you maybe redefining $articles in your template?

Oh I am…

  <?php foreach($page->children()->visible()->flip() as $article): ?>
      <article class="articles col-4 col-md-6 col-sm-12">

        <?php if($image = $article->coverimage()->toFile()): ?>
          <a class="image-link" href="<?= $article->url() ?>">
            <div class="articles-hero-image" style="background-image: url(<?= thumb($image, array())->url(); ?>)">
            </div>
          </a>
        <?php endif ?>

        <p class="articles-header"><a href="<?= $article->url() ?>"><?= $article->title()->html() ?></a></p>
        <div class="top-border-thin"></div>

        <div class="container">
          <div class="row align-items-start articles-links">
            <div class="col tags">

            <?php foreach(str::split($article->tags()) as $tag): ?>
                <a class="no-underline" href="<?php echo url('articles/' . url::paramsToString(['tag' => $tag])) ?>">
                    <?php echo html($tag) ?>
                </a>
            <?php endforeach ?>

            </div>
            <div class="col">
              <a class="articles-read-more" href="<?= $article->url() ?>">Read</a>
            </div>
          </div>
        </div>
      </article>
<?php endforeach ?>

Yeah, that doesn’t make sense, use the variables you have defined in the controller:

<?php foreach($articles as $article): ?>
 <?php foreach($tags as $tag): ?>
1 Like

Thanks that did the trick! :man_mechanic: