3

I'm attempting to build a filter that queries entries of a section based on two criteria: A selection of one or more categories (inclusive), and a single related entry (exclusive).

Specifically, the entries are from the "technologies" section. Each technology is related to one or more categories and a single "institution" entry.

The first step is to allow a user to, for example, select two categories (like "Information Technology" and "Medical Devices") and have the results be inclusive. Easily done with a relatedTo "or" parameter.

But next, I'd like the user to be able to select a single institution, and return only the previous results that are also associated with that institution (like "UW-Oshkosh"). This filter requires an additional relatedTo "and" parameter.

My attempt to add two different relatedTo parameters to the same query (detailed below) doesn't fly -- it will only honor the last relatedTo request:

{% set params = {
    section: 'technologies',
    limit: null
} %}

{% if technologyIds|length %}
    {% set paramTechnologyRelation = ['or'] %}
    {% for technologyId in technologyIds %}
        {% set paramTechnologyRelation = paramTechnologyRelation|merge([{ targetElement:technologyId }]) %}
    {% endfor %}
    {% set params = params|merge({relatedTo: paramTechnologyRelation}) %}
{% endif %}

{% if institutionId != "" %}
    {% set paramInstitutionRelation = ['and'] %}
    {% set paramInstitutionRelation = paramInstitutionRelation|merge([{ targetElement:institutionId }]) %}
    {% set params = params|merge({relatedTo: paramInstitutionRelation}) %}
{% endif %}

{% set technologyResults = craft.entries(params) %}

{% paginate technologyResults.limit(10) as technologies %}
    ...
{% endpaginate %}

I'm trying to think of other approaches -- Is is possible to run a second query on an array of entries? Any ideas on how to best approach this? Thanks for any insights.

5

Crisis averted, thanks largely in part to the versitile nature of the relatedTo targetElement processing. The relevation came that I could feed an array of category ids (inclusive) into that element, and use a single relatedTo "and" parameter overall to get my intended filtering:

{% set params = {
    section: 'technologies',
    limit: null
} %}

{% set paramRelation = ['and'] %}
{% if technologyIds|length %}
    {% set paramRelation = paramRelation|merge([{ targetElement:technologyIds }]) %}
{% endif %}
{% if institutionId != "" %}
    {% set paramRelation = paramRelation|merge([{ targetElement:institutionId }]) %}
{% endif %}
{% if paramRelation|length > 1 %}
    {% set params = params|merge({relatedTo: paramRelation}) %}
{% endif %}

{% set technologyResults = craft.entries(params) %}

{% paginate technologyResults.limit(10) as technologies %}
    ...
{% endpaginate %}

It's actually more simple this way as well, but I had initially underestimated how much heavy lifting the targetElement piece could do. Once again, Craft exceeds expectations. Thanks!

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.