How to get Tags filter working? Need some help

I want to move my WordPress site to Kirby and I am testing Kirby to learn how it works. So far, I like it :slight_smile:

I have some problems to get the tag-filters working. I have tried the cookbooks https://getkirby.com/docs/cookbook/content/filtering-with-tags.

And read some forum post (e.g. #1, #2) last few days, but I can’t figure out, what I am doing wrong.

So, some help and tipp are welcome

My YAML-File for one of the tags

fields:
  b17generalFilters:
    label: General Filters
    type: tags
    options: query
    query: page.siblings.pluck('b17generalFilters', ',', true)
    translate: false

The parent page slug is “b17”, which shows all the articles and template named “b17.php”.

<?php foreach( $articles as $article): ?>
                <article class="col-12 col-xxl-4">
                    <?php if($image = $article->b17cover()->toFile()): ?>
                        <a href="<?= $article->url() ?>"><img src="<?= $image->thumb(['format' => 'webp'])->url() ?>" alt="<?= $article->title() ?>" class="img-fluid"></a>
                    <?php endif ?>
                    <h2><?= $article->title()->html() ?></h2>
                    <a href="<?= $article->url() ?>">Read more…</a>
                </article>
            <?php endforeach ?>

Controller in site/controllers/b17.php

return function ($page) {

    $b17entries           = $page->children()->listed()->flip();
    $generalfilters  = $b17entries->pluck('b17generalFilters', ',', true);

    // add the tag filter
    if($generalfilter = param('b17generalFilter')) {
        $articles = $b17entries->filterBy('b17generalFilters', $generalfilter, ',');
    }
    // apply pagination
    $articles   = $b17entries->paginate(3);
    $pagination = $articles->pagination();

    return compact('articles', 'generalfilters', 'generalfilter', 'pagination');
};

The article template to show a single article, which created as child pages named “b17entry.php”. The values of the “b17generalFilter” are shown correctly.

This is the part of the code in "b17entry.php"

<h3>General Filters</h3>
        <ul>
            <?php foreach ($page->b17generalFilters()->split() as $filter): ?>
            <li><a href="<?= url('b17/b17generalFilters:' , ['params' => ['b17generalFilters' => urlencode($filter)]]) ?>"><?= $filter ?></a></li>
            <?php endforeach ?>
        </ul>

If I am clicking on the link of the filter. It will shows the parent page “b17” with all the articles. But not filtered with the tag-url, I have clicked.

I am not sure, if the URL is wrong and I have miss-understood some thing. The URL looks like this for the Filter value “B-17 Green Project”: https://domain.tld/b17/b17generalFilters:B-17+Green+Project

I would be happy if some one can help me :slight_smile:

My goal is to create multiple filters (e.g. #1, #2) and then search for articles after filtered.

But for the first step, I need to learn the Kirby basics :sweat_smile:

The generated URL is not correct. Should be

<li><a href="<?= url('b17' , ['params' => ['b17generalFilters' => urlencode($filter)]]) ?>"><?= $filter ?></a></li>

You want to get the b17 page url, not b17/b17generalFilters:. The parameter ist generated via the params array.

Thanks for quick reply. I have changed the code you mentioned and modified the parent page url part instead fixed slug “b17” before.

<h3>General Filters</h3>
        <ul>
            <?php foreach ($page->b17generalFilters()->split() as $filter): ?>
            <li><a href="<?= $page->parent()->url(['params' => ['b17generalFilters' => urlencode($filter)]]) ?>"><?= $filter ?></a></li>
            <?php endforeach ?>
        </ul>

Unfortunately, after clicking the links of the tags, the parent page still showing all articles, not these filtered by the url https://domain.tld/b17/b17generalFilters:B-17+Green+Project

The URL of the tags seems to be the same before. I’m at a loss. :face_with_monocle:

If you urlencode on one side, you have to urldecode on the other side, so in your controller:

urldecode(param('b17generalFilter'))

thank our for your support to a Kirby beginners.

I changed the part in my controller file
It still won’t working.

// add the tag filter
    if($generalfilter = urldecode(param('b17generalFilter'))) {
        $articles = $b17entries->filterBy('b17generalFilters', $generalfilter, ',');
    }

Maybe I should start from scratch again and try with starter-kit to figure out what I did wrong?
The tag filter for the notes working fine.

Just noticed there is another issue with your variables, you first define $b17entries, then $articles. But what is worse, you redefine $articles with $b17entries->paginate(3);, so your filter is undone again.

 $articles         = $page->children()->listed()->flip();
    $generalfilters  = $articles->pluck('b17generalFilters', ',', true);

    // add the tag filter
    if($generalfilter = param('b17generalFilter')) {
        $articles = $articles->filterBy('b17generalFilters', $generalfilter, ',');
    }
    // apply pagination
    $articles   = $articles->paginate(3);

All the other stuff should work, however.

Sorry, I only looked at the filter stuff first and overlooked the other issues.

Hello @texnixe , thank you for your support. I was busy last few days and couldn’t test you suggestion.But it not working after that changes.

I’ve also test to change this line

$articles = $b17entries->filterBy('b17generalFilters', $generalfilter, ',');

to.

$articles = $articles->filterBy('b17generalFilters', $generalfilter, ',');

Because of variable $b17entries. But, no success. I am sure I did something wrong because of inexperienced with Kirby and PHP.

Another Test by my self. I have created a complete new entry with General Filters (Tags) “Design” and “Test Filter” to avoid Cache issue.

If clicking the urls

The main page https://domain.tld/b17/ still returns all entries without filtering.

My next test: I have removed the controllers/b17.php

Added this code to template file templates/b17.php for hard-coding filter for “design”:

<?php
      $articles = $page->children()->filterBy('b17generalFilters', 'design', ',');
      var_dump($articles)
?>

The main page will show only the test entry I have created.

But, if I change the code to

<?php
        $articles = $page->children()->filterBy('b17generalFilters', 'test+filter', ',');
        var_dump($articles)
        ?>

It will show nothing. Not a single entry. But the tag “Test Filter” exist.

I am completely confuses and I understand, if you are confused too :sweat_smile:

This cannot work because your stored filter is 'test filternottest+filter. As I already wrote above, if you urlencodewhen printing the filter values in the template, you have tourldecode` when reading the filter in the controller (but forgot that in the last code snippet

// define articles
$articles        = $page->children()->listed()->flip();
$generalfilters  = $articles->pluck('b17generalFilters', ',', true);

// filter the articles
if($generalfilter = urldecode(param('b17generalFilter'))) {
    $articles = $articles->filterBy('b17generalFilters', $generalfilter, ',');
}
// apply pagination
 $articles   = $articles->paginate(3);