I want to try to learn how I can filter entries by multiple categories. I realise similar questions have been asked in the past, but all the answers are for specific use cases. All I want to do is filter entries by categories from more than one group. I've found code to set up a form that returns the URL with a string, but I understand that the string needs to be converted into an array. That's the part I can't work out how to do. So the form is like this:
{# Get the relevant entries #}
{% set categoryQuery = craft.app.request.getParam('category') %}
{% set categories = craft.categories.group('candidate').slug(categoryQuery) %}
{# Set the search parameters #}
{% set searchParams = {
relatedTo: categories,
section: 'candidate'
} %}
{# Get the relevant entries #}
{% set relevantEntries = craft.entries(searchParams) %}
<form action="{{ craft.app.request.absoluteUrl }}">
<p>Eye colour</p>
{% for category in craft.categories.group('eyeColour').all() %}
<input id="{{category.slug}}" type="checkbox" name="category[]" value="{{ category.slug }}" {{ category.slug in categoryQuery or categoryQuery == null ? 'checked'}}>
<label for="{{ category.slug }}" >{{ category.title }}</label>
{% endfor %}
<p>Hair colour</p>
{% for category in craft.categories.group('hairColour').all() %}
<input id="{{category.slug}}" type="checkbox" name="category[]" value="{{ category.slug }}" {{ category.slug in categoryQuery or categoryQuery == null ? 'checked'}}>
<label for="{{ category.slug }}" >{{ category.title }}</label>
{% endfor %}
<p><button type="submit">
Search
</button></p>
</form>
Which gives me a URL like this
/?category%5B%5D=blue&category%5B%5D=black