<p>You can conditionally add parameters to your criteria model. Check if there's a "area" parameter in your query string, e.g. <a href="http://example.com/barristers?area=7" rel="nofollow noreferrer">http://example.com/barristers?area=7</a> before adding the filter.</p>

<pre><code>{% set barristers = craft.entries.section('barristers').limit(null) %}

{% set query = craft.request.getQuery() %}

{% if query.area is defined %}
    {% set areaIds = craft.entries.section('practiceAreas').id(query.area).ids() %}
    {% set barristers = barristers.relatedTo({ targetElement: areaIds }) %}
{% endif %}
</code></pre>

<p>In case you need to add more than one <code>relatedTo</code> condition, have a look at this discussion:
<a href="https://craftcms.stackexchange.com/q/11870/125">Dynamic relatedTo queries</a></p>

CC BY-SA 3.0
2015-10-28T10:23:04.817Z
2015-10-28T10:29:47.627Z
2017-04-13T12:51:00.160Z
carlcs
<p>This is blowing my mind!</p>

<ul>
<li>I have a Section with handle <em>practiceAreas</em>.</li>
<li>I have a Section with handle <em>barristers</em> with approximately 100
entries.</li>
<li><em>barristers</em> has an Entries fieldType called <em>specialisms</em> which is linked to the <em>practiceAreas</em> Section.</li>
</ul>

<p>On the Barristers page I initially list ALL of the <em>barristers</em> entries, but I need to add a filter which lists the <em>practiceAreas</em> so when I user selects a specific Practice entry the page refreshes to show only the Barristers related to that particular practice.</p>

<p>I'm guessing this is done by adding an entry ID parameter to the URL when the user selects a Practice and then only pulling back the Barristers related to that ID when the page refreshes?</p>

<p>I don't know where to even start with this :/</p>

<p>Thanks for any help.</p>

<relations><filter>
Filter entries on a page based on related entry
# Filter entries on a page based on related entry

(Asked by: username_0 on 2015-10-28)

This is blowing my mind!

* I have a Section with handle *practiceAreas*.
* I have a Section with handle *barristers* with approximately 100
  entries.
* *barristers* has an Entries fieldType called *specialisms* which is linked to the *practiceAreas* Section.

On the Barristers page I initially list ALL of the *barristers* entries, but I need to add a filter which lists the *practiceAreas* so when I user selects a specific Practice entry the page refreshes to show only the Barristers related to that particular practice.

I'm guessing this is done by adding an entry ID parameter to the URL when the user selects a Practice and then only pulling back the Barristers related to that ID when the page refreshes?

I don't know where to even start with this :/

Thanks for any help.

**Tags:** relations, filter

---

## 1 answers

### Answer by username_1 on 2015-10-28. Score: 2

You can conditionally add parameters to your criteria model. Check if there's a "area" parameter in your query string, e.g. http://example.com/barristers?area=7 before adding the filter.
{% set barristers = craft.entries.section('barristers').limit(null) %}

{% set query = craft.request.getQuery() %}

{% if query.area is defined %}
    {% set areaIds = craft.entries.section('practiceAreas').id(query.area).ids() %}
    {% set barristers = barristers.relatedTo({ targetElement: areaIds }) %}
{% endif %}

In case you need to add more than one relatedTo condition, have a look at this discussion:
Dynamic relatedTo queries


---

