Happy new year everyone, hope you all had a great holiday
Attempting to filter pages by multiple URL params at the same time โ by a tags field and a pages field.
Have read up on all filtering articles and forum posts, I could find. However, still having some trouble.
Any tips would be much appreciated
Background
- I am making a restaurant guide website. There are two main content types pages (restaurants and cities) set up as a page with siblings (restaurant and city).
- Restaurants have different categories, and the restaurant/city association is made with a pages field.
- I would like to filter restaurants with multiple URL params at the same time (by category and city), eg. www.website.com/restaurants/category:burger/city:copenhagen
I have set up a restaurants.php controller that handles the param filtering. Categories are working, but I am having trouble understanding how to filter by city (pages field, see blueprint below).
return function($page, $kirby) {
// build restaurant page collection
$restaurants = page('restaurants')->children()->listed();
// get restaurant types and cities
$categories = $restaurants->pluck('categories', ',', true);
$cities = page('cities')->children()->listed();
// get params and search strings from url
$category = param('category');
$city = param('city');
$filtered = $restaurants
->when($category, function ($category) {
return $this->filterBy('categories', $category, ',');
})
->when($city, function($city) {
return $this->filter(function($child) use($page) {
return $child->city()->toPages()->has($city);
});
});
return [
'restaurants' => $filtered ?? null,
'type' => $type,
'city' => $city,
];
};
Have tried different tips from the forum, and the above is the latest attempt. Here I get a: Undefined variable $page. Maybe the when and return statement is wrong?
Here is how I have setup the restaurant/city association in my restaurant.yml blueprint.
title: Restaurant
fields:
text:
type: text
category:
type: multiselect
options:
burger: Burger
bar: Bar
โฆ
city:
type: pages
query: site.find('cities').children.listed
max: 1
Again, any tips would be much appreciated!
Best, Oliver