We're trying to filter by categories AND tags in the one form. However can't seem to get things working as we would like.
Essentially, we have a form that pulls in the tags and allows multiple filtering dropdowns. The below is out results page code, which outputs odd results. We need all params to match the entries categories/tags but it just doesn't work as expected. Where are we going wrong?
{# Get params #}
{% set mainCategory = craft.app.request.getParam(‘category’) %}
{% set season = craft.app.request.getParam(‘season’) %}
{% set age = craft.app.request.getParam(‘ages’) %}
{# Set arrays #}
{% set mainCat = mainCategory ? craft.categories.group(‘courseCategories’).slug(mainCategory).all() %}
{% set seasonCat = season ? craft.tags.group(‘season’).slug(season).all() %}
{% set ageCat = age ? craft.tags.group(‘ageRange’).slug(age).all() %}
{# Set relatedTo #}
{% set categoryParam = mainCat ? {targetElement: [‘and’]|merge(mainCat)} %}
{% set seasonParam = seasonCat ? {targetElement: [‘and’]|merge(seasonCat)} %}
{% set ageParam = ageCat ? {targetElement: [‘and’]|merge(ageCat)} %}
{# Get entries #}
{% set relationParams = [categoryParam, seasonParam, ageParam]|filter %}
{% set entries = craft.entries({
section: ‘courses’,
relatedTo: relationParams ? [‘and’]|merge(relationParams) : null
}) %}