<p>I think you are using the wrong string concatination operator. <code>~</code> is the twig string concatination operator (vs <code>.</code> for php and <code>+</code> for javascript).</p>

<p>Try this:</p>

<pre><code>{%
  set entries = craft.entries.section('schools').find({
   search: 'body:' ~ queryKeyword ~ ' categories:' ~ queryLocation,
  })
%}
</code></pre>

CC BY-SA 3.0
2015-04-19T23:44:32.117Z
Also, don't forget the space before "categories"... Although I'm not sure it's possible to conduct a search through related categories.
Lindsey D
CC BY-SA 3.0
2015-04-20T03:30:07.437Z
@LindseyD - good catch that space.
Marion Newlevant
CC BY-SA 3.0
2015-04-19T21:34:14.827Z
2015-04-20T03:29:48.570Z
2015-04-20T03:29:48.570Z
Marion Newlevant
<p>Assuming your "categories" field is in fact a <a href="http://buildwithcraft.com/docs/categories-fields" rel="nofollow">categories field</a> then I'd recommend to
use the <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param" rel="nofollow"><code>relatedTo</code></a> parameter to filter by that category instead of throwing everything into the <a href="http://buildwithcraft.com/docs/searching#templating" rel="nofollow"><code>search</code></a> parameter.</p>

<pre><code>{% set queryLocation = craft.request.getParam('l') %}
{% set queryKeyword = craft.request.getParam('q') %}

{# Set relationParam only if the param is set and a category with that slug exists #}
{% set category = craft.categories.group('location').slug(queryLocation).first() %}
{% set relationParam = queryLocation and category ? {targetElement: category} : '' %}

{# Set searchParam only if the param is set #}
{% set searchParam = queryKeyword ? 'body:' ~ queryKeyword : '' %}

{# Get the filtered entries #}
{% set entries = craft.entries.section('news').relatedTo(relationParam).search(searchParam) %}
</code></pre>

CC BY-SA 3.0
2015-04-20T09:16:33.827Z
2015-04-20T09:16:33.827Z
carlcs
<p>I'm having issues with getting results from a query. I have 2 search parameters which i have set to variables. I would like to match both of these to get results.</p>

<p>At the moment the query is returning zero results. I think this is because it is turning the variables into strings. Replacing the variables with hardcoded values does return results. </p>

<pre><code>{% set queryKeyword = craft.request.getParam('q') %}
{% set queryLocation = craft.request.getParam('l') %}

{%
    set entries = craft.entries.section('schools').find({
       search: 'body:queryKeyword categories:queryLocation'
    })
%}
</code></pre>

<p>I have tried:</p>

<pre><code>{%
    set entries = craft.entries.section('schools').find({
       search: 'body:' + queryKeyword + 'categories:' + queryLocation,
    })
%}
</code></pre>

<p>This doesn't work. </p>

<p>I'm very new to Craft CMS and am not very experienced in PHP/MySQL so any help would be greatly appreciated. Thanks!</p>

<entries><search><queries>
How to query using variables
# How to query using variables

(Asked by: username_0 on 2015-04-19)

I'm having issues with getting results from a query. I have 2 search parameters which i have set to variables. I would like to match both of these to get results.
At the moment the query is returning zero results. I think this is because it is turning the variables into strings. Replacing the variables with hardcoded values does return results. 
{% set queryKeyword = craft.request.getParam('q') %}
{% set queryLocation = craft.request.getParam('l') %}

{%
    set entries = craft.entries.section('schools').find({
       search: 'body:queryKeyword categories:queryLocation'
    })
%}

I have tried:
{%
    set entries = craft.entries.section('schools').find({
       search: 'body:' + queryKeyword + 'categories:' + queryLocation,
    })
%}

This doesn't work. 
I'm very new to Craft CMS and am not very experienced in PHP/MySQL so any help would be greatly appreciated. Thanks!

**Tags:** entries, search, queries

---

## 2 answers

### Answer by username_1 on 2015-04-19. Score: 5 | Accepted answer

I think you are using the wrong string concatination operator. ~ is the twig string concatination operator (vs . for php and + for javascript).
Try this:
{%
  set entries = craft.entries.section('schools').find({
   search: 'body:' ~ queryKeyword ~ ' categories:' ~ queryLocation,
  })
%}

> **Comments:**
> **username_2**: Also, don't forget the space before "categories"... Although I'm not sure it's possible to conduct a search through related categories.  
> **username_1**: @LindseyD - good catch that space.  

---

### Answer by username_3 on 2015-04-20. Score: 1

Assuming your "categories" field is in fact a categories field then I'd recommend to
use the relatedTo parameter to filter by that category instead of throwing everything into the search parameter.
{% set queryLocation = craft.request.getParam('l') %}
{% set queryKeyword = craft.request.getParam('q') %}

{# Set relationParam only if the param is set and a category with that slug exists #}
{% set category = craft.categories.group('location').slug(queryLocation).first() %}
{% set relationParam = queryLocation and category ? {targetElement: category} : '' %}

{# Set searchParam only if the param is set #}
{% set searchParam = queryKeyword ? 'body:' ~ queryKeyword : '' %}

{# Get the filtered entries #}
{% set entries = craft.entries.section('news').relatedTo(relationParam).search(searchParam) %}


---

