<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>

CC BY-SA 3.0
2016-01-19T20:27:25.597Z
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?
Rob
CC BY-SA 3.0
2016-01-19T21:13:09.870Z
Yes, this is right, `getQueryString()` returns the complete query string and `getQuery()` (without a param) returns the query string converted to an array.
carlcs
CC BY-SA 3.0
2015-10-12T16:00:49.303Z
2015-10-12T16:00:49.303Z
carlcs
<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>

<entries>
Dynamic relatedTo queries
# 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.  

---

