<p>100% untested, but try something like this.</p>

<p>You're basically creating an array of all of your <code>search</code> criteria, then joining them into one <code>.search()</code> call at the end as a string separated with a space.  That's because Craft does "and" <a href="https://craftcms.com/docs/searching" rel="nofollow noreferrer">searching</a> with multiple terms by default.</p>

<pre><code>{% set req = craft.request %}

{% set results = craft.entries.section("properties") %}

{% set params = ['location', 'min-price', 'max-price', 'status'] %}
{% set query = {} %}
{% set search = [] %}

{% for param in params %}
    {% set key = param | camel %}
    {% set value = req.getParam(param) %}
    {% set query = query | merge({ (key):(value) }) %}
{% endfor %}

{% if query.location %}
    {% set search = search|merge([query.location]) %}
{% endif %}

{% if query.minPrice or query.maxPrice %}
    {% set min = query.minPrice ? query.minPrice : '' %}
    {% set max = query.maxPrice ? query.maxPrice : '' %}
    {% set search = search|merge(["price:" ~ '500000']) %}
{% endif %}

{% if query.status %}
    {% set search = search|merge(['statusCategory:' ~ query.status]) %}
{% endif %}

{% if search|length %}
    {% set results = results.search(search|join(' ') %}
{% endif %}

{% for entry in results %}
    ...
</code></pre>

CC BY-SA 3.0
2017-09-20T22:23:13.227Z
Ah that makes sense. Appreciate it!
Joshua
CC BY-SA 3.0
2017-09-20T21:31:31.943Z
2017-09-20T21:31:31.943Z
Brad Bell
<p>I'm filtering entries based off a searchbar with multiple parameters, and I'm building up an entries filter based off of them. Everything works great except for having more than one .search functions.</p>

<pre><code>{% set req = craft.request %}

{% set results = craft.entries.section("properties") %}

{% set params = ['location', 'min-price', 'max-price', 'status'] %}
{% set query = {} %}

{% for param in params %}
  {% set key = param | camel %}
  {% set value = req.getParam(param) %}
  {% set query = query | merge({ (key):(value) }) %}
{% endfor %}

{% if query.location %}
  {% set results = results.search(query.location) %}
{% endif %}

{% if query.minPrice or query.maxPrice %}
  {% set min = query.minPrice ? query.minPrice : '' %}
  {% set max = query.maxPrice ? query.maxPrice : '' %}
  {% set results = results.search("price:" ~ '500000') %}
{% endif %}

{% if query.status %}
  {% set results = results.search('statusCategory:' ~ query.status) %}
{% endif %}

{% for entry in results %}
...
</code></pre>

<p>I've removed the whole min/max part just to simplify things, but it seems that if there is both a location and min/max price parameter, whichever one is last overrides the first. How can I combine these search parameters?</p>

<form><search>
How do I search craft.entries with multiple parameters?
# How do I search craft.entries with multiple parameters?

(Asked by: username_0 on 2017-09-20)

I'm filtering entries based off a searchbar with multiple parameters, and I'm building up an entries filter based off of them. Everything works great except for having more than one .search functions.
{% set req = craft.request %}

{% set results = craft.entries.section("properties") %}

{% set params = ['location', 'min-price', 'max-price', 'status'] %}
{% set query = {} %}

{% for param in params %}
  {% set key = param | camel %}
  {% set value = req.getParam(param) %}
  {% set query = query | merge({ (key):(value) }) %}
{% endfor %}

{% if query.location %}
  {% set results = results.search(query.location) %}
{% endif %}

{% if query.minPrice or query.maxPrice %}
  {% set min = query.minPrice ? query.minPrice : '' %}
  {% set max = query.maxPrice ? query.maxPrice : '' %}
  {% set results = results.search("price:" ~ '500000') %}
{% endif %}

{% if query.status %}
  {% set results = results.search('statusCategory:' ~ query.status) %}
{% endif %}

{% for entry in results %}
...

I've removed the whole min/max part just to simplify things, but it seems that if there is both a location and min/max price parameter, whichever one is last overrides the first. How can I combine these search parameters?

**Tags:** form, search

---

## 1 answers

### Answer by username_1 on 2017-09-20. Score: 2

100% untested, but try something like this.
You're basically creating an array of all of your search criteria, then joining them into one .search() call at the end as a string separated with a space.  That's because Craft does "and" searching with multiple terms by default.
{% set req = craft.request %}

{% set results = craft.entries.section("properties") %}

{% set params = ['location', 'min-price', 'max-price', 'status'] %}
{% set query = {} %}
{% set search = [] %}

{% for param in params %}
    {% set key = param | camel %}
    {% set value = req.getParam(param) %}
    {% set query = query | merge({ (key):(value) }) %}
{% endfor %}

{% if query.location %}
    {% set search = search|merge([query.location]) %}
{% endif %}

{% if query.minPrice or query.maxPrice %}
    {% set min = query.minPrice ? query.minPrice : '' %}
    {% set max = query.maxPrice ? query.maxPrice : '' %}
    {% set search = search|merge(["price:" ~ '500000']) %}
{% endif %}

{% if query.status %}
    {% set search = search|merge(['statusCategory:' ~ query.status]) %}
{% endif %}

{% if search|length %}
    {% set results = results.search(search|join(' ') %}
{% endif %}

{% for entry in results %}
    ...

> **Comments:**
> **username_0**: Ah that makes sense. Appreciate it!  

---

