in the docs I learned how to use filters, but I didn’t understand how to use them in my specific case. I have an online shop with products by different artists. Then I have subpages of all artists with information about them. This artist page should now show all products (subpages) from this artist. In the blueprints of product and artist pages, I set up a field for an artist id. In this case simply the full name of the artist (like johndoe).
My artist template needs a filter which is created by himself on every artist subpage. I can do the following:
But in this case on every artist page there would be the work of John Doe. Is it possible that Kirby puts in the correct artist id matching to the specific artist? So it lists all subpages with the same artist id? Or is there another way to do this in Kirby?
You can pass an artist as argument to your filter. Usually, you will probably filter by an URL parameter, then you’d get your parameter and pass that in instead of the hardcoded johndoe.
Sorry, I hove no idea how PHP works I love Kirby because I can build websites without knowing much about PHP, but when I need some deeper functions I have to ask.
First I don’t know what I should do with the first code window. echo param('tag'); Is this an example to show how it works, or do I have to put this line somewhere in my code, if yes, where?
Then can I use my code like as above?
And at which point do I have to put the third code example from the doc. Can I replace all the tags and $tag codes with artistid and $artistid? Sometimes I don’t know which tag/variable is from kirby and which one can I name how I want
I am not sure if filtering by URL is that what I need right now.
This works as expected, but I need something dynamic. <?php foreach ($page->children()->filterBy('artistid', '==', 'johndoe' as $product): ?>
I want something like this, but this isn’t working. Can you explain why? <?php foreach ($page->children()->filterBy('artistid', '==', $page->artistid()) as $product): ?>
$page->artistid() text field contains exactly johndoe
Home/Shop
– Product 1
– Product 2
– Product 3
– ...
Artists/Overview
– Artist 1
– Artist 2
– Artist 3
– ...
2. Which template
I want the filter in the template of an artist page. It should show all products by the specific artist. The artist page shows some facts about the artist like bio and last exhibitions and underneath the products. So the user doesn’t have to apply the filter via a link or something. It should load with the page.
3. Argument
In each product page blueprint is a text field which I called artistid. The same artistid text field is in the artist blueprint. So, all products by John Doe have an artistid johndoe and the artist page about John Doe has also the same artist id. I thought, this could be the way how the filter knows on which page it is and which products it has to filter.
This is why I wrote: filterBy('artistid', '==', $page->artistid())
I hope you get me know, because I can’t explain better
Adding the artistid manually in two places in a text field is not what I’d do, though.
You can use a pages field instead in the product page blueprint where you query the artists instead. It will then store the artist page id in the content file and you can use that to filter by.