<p>You have two different options. You can either use Crafts <a href="https://docs.craftcms.com/v2/searching.html" rel="nofollow noreferrer">search</a> so your string would be <code>listingAgent:*searchterm* OR coListingAgent:*searchterm*</code> or <code>listingAgent:searchterm OR coListingAgent:searchterm</code> if it should be an exact search</p>

<pre><code>{% set results = craft.entries()
    .search({
        query: 'listingAgent:*' ~ getAgent ~ '* OR  coListingAgent:*' ~ getAgent  ~ '*',
        subLeft: true,
        subRight: true
    })
    .all() 
%}
</code></pre>

<p>or you query correctly for the field values but I would prefer to do that in PHP instead of Twig</p>

CC BY-SA 4.0
2018-05-02T14:19:08.577Z
Thanks for your help. Since this is just one parameter of a more complex search, how could I put that into syntax in my params variable (from my original post)?
triplethreat23
CC BY-SA 4.0
2018-05-02T14:21:32.353Z
Why do you even want to do that. Twigs `|merge` function is so resource heavy I would suggest you not to use it too much. You can easily check if the parameter is set, if so attach it to the query
Robin Schambach
CC BY-SA 4.0
2018-05-02T15:35:09.650Z
I guess overall I just don't understand the method you're describing. Sorry I'm relatively new to this.
triplethreat23
CC BY-SA 4.0
2018-05-02T15:40:23.707Z
`craft.entries` does not return an array of elements but an object of type `query` you can call all the functions you would like in the query in order to attach your criterias. You can do `{% set query = craft.entries.section('whatever') %}` and then append certain values `{% do query.search(getAgent) %}` and to fetch the entries `{% set entries = query.all() %}`
Robin Schambach
CC BY-SA 4.0
2018-05-02T16:47:26.923Z
Thank you. That makes sense. Could you help me with the query syntax? I have query: 'listingAgent:* ~ address ~ '* OR  coListingAgent:*' ~ address  ~ '*', and I'm getting an error Unexpected character " ' ".
triplethreat23
CC BY-SA 4.0
2018-05-03T08:43:26.283Z
@triplethreat23 I had a syntax error in my code (missing one `'`) and you copied it
Robin Schambach
CC BY-SA 4.0
2018-05-02T14:06:02.067Z
2018-05-03T08:43:04.833Z
2018-05-03T08:43:04.833Z
Robin Schambach
<p>I'm building a search form with filters. This is one of the filters.
I would like to use the parameter "agent" and have it search a text field called "listingAgent" and "coListingAgent" to check for a match either place.  I tried the code below, but can't seem to get it to work.</p>

<pre><code>    {% set getAgent = craft.request.getParam('agent') %}
    {% if getAgent %}
        {% set params = params|merge({ 'listingAgent':getAgent, 'coListingAgent':getAgent }) %}
    {% endif %}
</code></pre>

<p>additional info:
I noticed that setting the params twice (like below) is looking for a match in both fields. I am looking for a match in either field. Does anyone know how to go about doing this?</p>

<pre><code>{% set getAgent = craft.request.getParam('agent') %}
    {% if getAgent %}
        {% set params = params|merge({'listingAgent':getAgent}) %}
        {% set params = params|merge({'coListingAgent':getAgent}) %}
    {% endif %}

{% set entries = craft.entries(params).search() %}
</code></pre>

CC BY-SA 4.0
2018-05-02T05:05:17.407Z
Have you read the docs? https://docs.craftcms.com/v2/searching.html
Robin Schambach
CC BY-SA 4.0
2018-05-02T13:50:21.127Z
I did and I got the impression I could use "OR", I tried  {% set params = params|merge({'listingAgent':getAgent} OR {'coListingAgent':getAgent})and get the error back Arguments must be separated by a comma. Unexpected token "name" of value "OR" ("punctuation" expected with value ",").
triplethreat23
<search><merge>
Search Multiple Fields Same Parameter
# Search Multiple Fields Same Parameter

(Asked by: username_0 on 2018-05-01)

I'm building a search form with filters. This is one of the filters.
I would like to use the parameter "agent" and have it search a text field called "listingAgent" and "coListingAgent" to check for a match either place.  I tried the code below, but can't seem to get it to work.
    {% set getAgent = craft.request.getParam('agent') %}
    {% if getAgent %}
        {% set params = params|merge({ 'listingAgent':getAgent, 'coListingAgent':getAgent }) %}
    {% endif %}

additional info:
I noticed that setting the params twice (like below) is looking for a match in both fields. I am looking for a match in either field. Does anyone know how to go about doing this?
{% set getAgent = craft.request.getParam('agent') %}
    {% if getAgent %}
        {% set params = params|merge({'listingAgent':getAgent}) %}
        {% set params = params|merge({'coListingAgent':getAgent}) %}
    {% endif %}

{% set entries = craft.entries(params).search() %}

**Tags:** search, merge
> **Comments:**
> **username_1**: Have you read the docs? https://docs.craftcms.com/v2/searching.html  
> **username_0**: I did and I got the impression I could use "OR", I tried  {% set params = params|merge({'listingAgent':getAgent} OR {'coListingAgent':getAgent})and get the error back Arguments must be separated by a comma. Unexpected token "name" of value "OR" ("punctuation" expected with value ",").  

---

## 1 answers

### Answer by username_1 on 2018-05-02. Score: 1

You have two different options. You can either use Crafts search so your string would be listingAgent:*searchterm* OR coListingAgent:*searchterm* or listingAgent:searchterm OR coListingAgent:searchterm if it should be an exact search
{% set results = craft.entries()
    .search({
        query: 'listingAgent:*' ~ getAgent ~ '* OR  coListingAgent:*' ~ getAgent  ~ '*',
        subLeft: true,
        subRight: true
    })
    .all() 
%}

or you query correctly for the field values but I would prefer to do that in PHP instead of Twig

> **Comments:**
> **username_0**: Thanks for your help. Since this is just one parameter of a more complex search, how could I put that into syntax in my params variable (from my original post)?  
> **username_1**: Why do you even want to do that. Twigs `|merge` function is so resource heavy I would suggest you not to use it too much. You can easily check if the parameter is set, if so attach it to the query  
> **username_0**: I guess overall I just don't understand the method you're describing. Sorry I'm relatively new to this.  
> **username_1**: `craft.entries` does not return an array of elements but an object of type `query` you can call all the functions you would like in the query in order to attach your criterias. You can do `{% set query = craft.entries.section('whatever') %}` and then append certain values `{% do query.search(getAgent) %}` and to fetch the entries `{% set entries = query.all() %}`  
> **username_0**: Thank you. That makes sense. Could you help me with the query syntax? I have query: 'listingAgent:* ~ address ~ '* OR  coListingAgent:*' ~ address  ~ '*', and I'm getting an error Unexpected character " ' ".  
> **username_1**: @triplethreat23 I had a syntax error in my code (missing one `'`) and you copied it  

---

