2

This answer here helped me get multiple same-name parameters to filter entries: (ex: ?recipe=healthy&recipe=salad&recipe=dinner)

Now I'm looking for a way to get multiple different parameters to help sort entries on the front-end. Something like:
?collection=healthy&ingredient=salad&meal=dinner

The code I have now is this (pretty much verbatim what came from the answer linked above):

{# Get search parameters from the URL query string #}
{% set collectionQuery = craft.request.getParam('collection') %}

{# Start an array of search parameters (relationParam) with 'and' to match ALL categories #}
{% set relationParam = ['and'] %}

{# Find categories that match the slugs in your query string... #}
{% for querySlug in collectionQuery %}
  {% set collectionCategory = craft.categories.group('recipes').slug(querySlug) %}

  {# And merge them into the relationParam array. #}
  {% set relationParam = relationParam|merge({targetElement: collectionCategory}) %}
{% endfor %}

{# Avoid passing an array with `['and']` only #}
{% if relationParam|length < 2 %}
  {% set relationParam = '' %}
{% endif %}

{% set searchParams = {
  relatedTo:  relationParam,
  section: 'generalContent',
  type: 'recipes'
} %}

<form action="/recipes">
  {% for category in craft.categories.group('recipes').level('1') %}
    <div style="display: inline-block;">
    <select name="{{ category.slug }}[]" onchange="this.form.submit()">
      <option name="{{ category.slug }}[]" value="">Any {{ category.title }}</option>

      {% for categoryChild in category.children() %}
        <option name="{{ category.slug }}[]" value="{{ categoryChild.slug }}"{{ categoryChild.slug in collectionQuery  ? 'selected'}}>{{ categoryChild.title }}</option>
      {% endfor %}
    </select>
    </div>
  {% endfor %}
</form>

{% paginate craft.entries(searchParams) as entries %}
    ...
{% endpaginate %}
1

You need to get each parameter from the URL separately and repeat the steps you already did for "collection":

  • Get search parameters from the URL query string
  • Find categories that match the slugs in your query string
  • And merge them into the relationParam array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.