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

<ul>
<li>Get search parameters from the URL query string</li>
<li>Find categories that match the slugs in your query string</li>
<li>And merge them into the relationParam array.</li>
</ul>

CC BY-SA 3.0
2015-06-15T22:10:23.370Z
2015-06-15T22:10:23.370Z
carlcs
<p>This answer <a href="https://craftcms.stackexchange.com/questions/9081/how-to-filter-entries-by-clicking-multiple-categories-on-front-end">here</a> helped me get multiple same-name parameters to filter entries: 
(ex: <code>?recipe=healthy&amp;recipe=salad&amp;recipe=dinner</code>)</p>

<p>Now I'm looking for a way to get multiple <em>different</em> parameters to help sort entries on the front-end. Something like:<br>
<code>?collection=healthy&amp;ingredient=salad&amp;meal=dinner</code></p>

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

<pre><code>{# 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 &lt; 2 %}
  {% set relationParam = '' %}
{% endif %}

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

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

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

{% paginate craft.entries(searchParams) as entries %}
    ...
{% endpaginate %}
</code></pre>

<filter><params>
Filtering entries by multiple URL parameters on the front end
# Filtering entries by multiple URL parameters on the front end

(Asked by: username_0 on 2015-06-10)

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'
} %}


  {% for category in craft.categories.group('recipes').level('1') %}
    
    
      Any {{ category.title }}

      {% for categoryChild in category.children() %}
        {{ categoryChild.title }}
      {% endfor %}
    
    
  {% endfor %}


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

**Tags:** filter, params


---

## 1 answers

### Answer by username_1 on 2015-06-15. Score: 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.


---

