Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-19T23:44:32.117Z
Answers.Text: Also, don't forget the space before "categories"... Although I'm not sure it's possible to conduct a search through related categories.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-20T03:30:07.437Z
Answers.Text: @LindseyD - good catch that space.
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-19T21:34:14.827Z
Answers.LastActivityDate: 2015-04-20T03:29:48.570Z
Answers.LastEditDate: 2015-04-20T03:29:48.570Z
Answers.UserDisplayName: Marion Newlevant
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-20T09:16:33.827Z
Answers.LastActivityDate: 2015-04-20T09:16:33.827Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <entries><search><queries>
Title: How to query using variables
ThreadText: # 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**: @username_2 - good catch that space.  
  
  ---
  
  ### Answer by username_3 on 2015-04-20. Score: 1
  
  Assuming your "categories" field is in fact a [categories field](http://buildwithcraft.com/docs/categories-fields) then I'd recommend to
  use the [`relatedTo`](http://buildwithcraft.com/docs/relations#the-relatedTo-param) parameter to filter by that category instead of throwing everything into the [`search`](http://buildwithcraft.com/docs/searching#templating) 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) %}
  
  ```
  
  
  ---
  
  