Answers.Body: <p>You can build the array for your criteria model's relatedTo parameter step for step before adding it:</p>
  
  <pre><code>{# Initial criteria model #}
  {% set exhibitions = craft.entries.section('exhibitions').limit(null) %}
  
  {# Compose relatedTo parameter #}
  {% set relatedToParam = ['and'] %}
  
  {% if query.art is defined %}
      {% set catIds = craft.categories.group('artCategories').id(query.art).ids() %}
      {% set relatedToParam = relatedToParam|merge([{ targetElement: catIds }]) %}
  {% endif %}
  
  {% if query.location is defined %}
      {% set catIds = craft.categories.group('locationCategories').id(query.location).ids() %}
      {% set relatedToParam = relatedToParam|merge([{ targetElement: catIds }]) %}
  {% endif %}
  
  {# Add additional criteria. Avoid passing an array with 'and' only #}
  {% if relatedToParam|length &gt; 1 %}
      {% set exhibitions = exhibitions.relatedTo(relatedToParam) %}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-19T20:27:25.597Z
Answers.Text: Just adding a [link back to this post](http://craftcms.stackexchange.com/questions/12140/filter-entries-on-a-page-based-on-related-entry/12141#12141), as it explains how you're setting `craft.request.getQuery()` and accessing it's parameter `query.art` (I haven't seen this method before). Does [`getQueryString()`](https://craftcms.com/docs/templating/craft.request#getQueryString) do the same thing?
Answers.UserDisplayName: Rob
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-19T21:13:09.870Z
Answers.Text: Yes, this is right, `getQueryString()` returns the complete query string and `getQuery()` (without a param) returns the query string converted to an array.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-12T16:00:49.303Z
Answers.LastActivityDate: 2015-10-12T16:00:49.303Z
Answers.UserDisplayName: carlcs
Body: <p>I've got an advanced search form which culminates in:</p>
  
  <pre><code>craft.entries.section('whatsOn').relatedTo(
      'and',
      { element: artCategories },
      { element: locationCategories },
      { element: bbfcCategories }
  ).search(searchTerm).order('score').limit(8)
  </code></pre>
  
  <p>This works, but only if the item being searched for has populated <code>artCategories</code>, <code>locationCategories</code>, and <code>bbfcCategories</code>.</p>
  
  <p>Any of those are optional; i.e., the <code>bbfcCategories</code> line may not be needed at all... and likewise the searchTerm - is there a way to do this dynamically?</p>
  
Tags: <entries>
Title: Dynamic relatedTo queries
ThreadText: # Dynamic relatedTo queries
  
  (Asked by: username_0 on 2015-10-12)
  
  I've got an advanced search form which culminates in:
  
  ```
  craft.entries.section('whatsOn').relatedTo(
      'and',
      { element: artCategories },
      { element: locationCategories },
      { element: bbfcCategories }
  ).search(searchTerm).order('score').limit(8)
  
  ```
  
  This works, but only if the item being searched for has populated `artCategories`, `locationCategories`, and `bbfcCategories`.
  
  Any of those are optional; i.e., the `bbfcCategories` line may not be needed at all... and likewise the searchTerm - is there a way to do this dynamically?
  
  **Tags:** entries
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-10-12. Score: 5 | Accepted answer
  
  You can build the array for your criteria model's relatedTo parameter step for step before adding it:
  
  ```
  {# Initial criteria model #}
  {% set exhibitions = craft.entries.section('exhibitions').limit(null) %}
  
  {# Compose relatedTo parameter #}
  {% set relatedToParam = ['and'] %}
  
  {% if query.art is defined %}
      {% set catIds = craft.categories.group('artCategories').id(query.art).ids() %}
      {% set relatedToParam = relatedToParam|merge([{ targetElement: catIds }]) %}
  {% endif %}
  
  {% if query.location is defined %}
      {% set catIds = craft.categories.group('locationCategories').id(query.location).ids() %}
      {% set relatedToParam = relatedToParam|merge([{ targetElement: catIds }]) %}
  {% endif %}
  
  {# Add additional criteria. Avoid passing an array with 'and' only #}
  {% if relatedToParam|length > 1 %}
      {% set exhibitions = exhibitions.relatedTo(relatedToParam) %}
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_2**: Just adding a [link back to this post](http://craftcms.stackexchange.com/questions/12140/filter-entries-on-a-page-based-on-related-entry/12141#12141), as it explains how you're setting `craft.request.getQuery()` and accessing it's parameter `query.art` (I haven't seen this method before). Does [`getQueryString()`](https://craftcms.com/docs/templating/craft.request#getQueryString) do the same thing?  
  > **username_1**: Yes, this is right, `getQueryString()` returns the complete query string and `getQuery()` (without a param) returns the query string converted to an array.  
  
  ---
  
  