1

I have a multiple dropdown filters that lets users pick the category that they want to display results for below. See my previous question Search entries by location or name for screenshot.

So I have one working, but I need some guidance on how to do the code for the other filters as well, as currently my code for linking up to the second filter is stopping the first filter from working correctly. So clearly I'm doing it wrong.

So I have four dropdown filters, but I'll simplify it to two filters for this example. The filters are named name="county1" and name="development1" in their respective tags.

And then I'm using the following to access them:

{% set query = craft.request.getParam('county1') %}
{% set query = craft.request.getParam('development1') %}
{% set queryFilter = craft.categories.group('developments').search(query).order('title asc') %}

{% for category in queryFilter %}

....markup...

{% endfor %}

Can someone tell me what i need to do to fix this so that the filters don't interfere with each other?

  • Are you trying to filter for a category or entries based on a category selected? Does your drop-downs contain categories from the group 'developments'? – aran Jan 16 '17 at 4:29
  • The first dropdown holds categories from a category group called counties and the second dropdown holds categories from a category group called developments. The filters themselves are showing the correct data and the first dropdown is creating the right results when selected, but the problem I'm having is that the second dropdown is not outputting the correct results. So I'm assuming the line {% set query = craft.request.getParam('development1') %} and possibly that I need to set a second queryFilter row with different name? – mention79 Jan 16 '17 at 9:23
  • Are you trying to filter by both at the same time or just one filter at a time? – aran Jan 16 '17 at 15:10
  • just one at a time is all I need – mention79 Jan 16 '17 at 15:12
1

If your goal is to filter by one drop-down value at a time then you could use an if statement to check if the passed value is set for each drop-down. This will only work if each drop-down has an option that is blank or it submits on select so that the other drop-down is not set.

{% set query = "" %}
{% if craft.request.getParam('country1') and 
      craft.request.getParam('country1') is not empty %}
    {% set query = craft.request.getParam('country1') %}
{% elseif craft.request.getParam('development1') and 
          craft.request.getParam('development1') is not empty %}
    {% set query = craft.request.getParam('development1') %}
{% endif %}

{% for category in queryFilter %}

....markup...

{% endfor %}
|improve this answer|||||
  • Excellent, that has it working, but only if I turn off dev mode as in dev mode it says Variable "query" does not exist So is there any ideas how to fix that? If not I can just turn off dev mode. – mention79 Jan 16 '17 at 15:32
  • 1
    I have updated the answer – aran Jan 16 '17 at 15:33

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.