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() %}
  • Have you read the docs? docs.craftcms.com/v2/searching.html May 2 '18 at 5:05
  • 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 ","). May 2 '18 at 13:50

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()
        query: 'listingAgent:*' ~ getAgent ~ '* OR  coListingAgent:*' ~ getAgent  ~ '*',
        subLeft: true,
        subRight: true

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

  • 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)? May 2 '18 at 14:19
  • 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 May 2 '18 at 14:21
  • I guess overall I just don't understand the method you're describing. Sorry I'm relatively new to this. May 2 '18 at 15:35
  • 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() %} May 2 '18 at 15:40
  • 1
    @triplethreat23 I had a syntax error in my code (missing one ') and you copied it May 3 '18 at 8:43

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.