0

I have a search form that contains a dropdown based on categories and a text field to search by title. This works the way it should. Now I need to add an extra text field to search by location as well but when I do that the search form doesn't work anymore.

What I have is:

{# defaults #}
    {% set queryParams = {
      section: 'klanten',
      order: 'title asc',
    } %}


    {# Build a category array beginning with 'and' and add targetElement objects with selected categories Ids to it #}
    {% set categoriesArray = ['and'] %}

    {% set searchCat = craft.request.getParam('cat') %}
    {% if searchCat is not empty %}
      {% set searchThemeCat = craft.categories.slug(searchCat).ids() %}
      {% set categoriesArray = categoriesArray|merge([{
        targetElement: searchThemeCat,
      }]) %}
    {% endif %}


    {% if categoriesArray|length > 1 %}

      {% set queryParams = queryParams|merge({
        relatedTo: categoriesArray
      }) %}

    {% endif %}

    {# search #}
    {% set searchQ = craft.request.getParam('q') %}
    {% if searchQ is not empty %}
      {% set queryParams = queryParams|merge({
        search: {
          query: 'title:' ~ searchQ,
          subLeft: true,
          subRight: true,
        }
      }) %}
    {% endif %}

{% set klantEntries = craft.entries(queryParams) %}

Now I thought adding the following would allow me to search by location as well but instead it says 'no results are found for ""' which means that it search query is empty, I guess?

{% set searchL = craft.request.getParam('loc') %}
    {% if searchL is not empty %}
      {% set queryParams = queryParams|merge({
        search: {
          query: 'klantLocation:' ~ searchL,
          subLeft: true,
          subRight: true,
        }
      }) %}
    {% endif %}

I don't really know how to fix it, I've been looking through a lot of examples but I can't get it to work, any help would be appreciated

5
  • What type of field is klantLocation?
    – Brad Bell
    Jan 16, 2017 at 23:55
  • @BradBell it's a plain text field
    – Mosh
    Jan 17, 2017 at 11:18
  • You're sure searchL is what you're expecting it to be? I'd also enable devMode, run the search, then find the SQL that's being generated in craft/storage/runtime/logs and make sure it looks correct for the data you're looking for.
    – Brad Bell
    Jan 18, 2017 at 3:34
  • @BradBell Thank you, telling me to check the logs made me realise my field wasn't called 'klantLocation' but 'klantLocatie'. Pretty dumb mistake, but thank you for your time and help
    – Mosh
    Jan 18, 2017 at 20:03
  • Glad you're all sorted!
    – Brad Bell
    Jan 18, 2017 at 21:17

1 Answer 1

2

I found the problem, which is pretty ridiculous but something that probably happens to other developers who are using multiple languages.. (I hope).

In

query: 'klantLocation:' ~ searchL,

I had to replace klantLocation with klantLocatie

Pretty dumb mistake but hopefully someone else will find some help in my mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.