4

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.