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
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 incraft/storage/runtime/logs
and make sure it looks correct for the data you're looking for.