1

I have a dropdown search query I have implemented using this blog (https://www.webstoemp.com/blog/combined-searches-and-filters-craft-cms)

This works excellent for searching my three categories and it's very fast.

{{% set searchContactFor = craft.request.getParam('contactFor') %}

{% if searchContactFor is not empty %}
    {% set searchContactForCat = craft.categories.slug(searchContactFor).ids() %}
    {% set categoriesArray = categoriesArray|merge([{
        targetElement: searchContactForCat,
    }]) %}
{% endif %}}

However, I have a category I need to search call 'institution', which is nested inside a MatrixBlock 'position'. When I attempted to use the same code for searching my other categories it is unable to retrieve the slug.ids(). I've been using the Kint debugger and I'm still unable to crack this one. I am assuming I need to reached into the Matrix block 'position' prior to grabbing the 'institution' slug.ids(). I have several hacks that work but are really terrible on query performance. Does anyone have a simple solution for this that I am missing? Here is an example of an expensive query. As you can see it's pulling back all the member.ids and then matching them to the intuition. Very Slow!

{% set searchInstitution = craft.request.getParam('institution')|lower %}
{% set searchInstitution = searchInstitution|split(',') %}
{% set memberIds = [] %}

{% if searchInstitution is not empty %}
    {% for entry in craft.entries.section('member').limit(null) %}

        {% set found = false %}

        {% for block in entry.position %}
            {% if block.institution.first()|lower in searchInstitution %}
                {% set found = true %}
            {% endif %}
        {% endfor %}

        {% if found %}
            {% set memberIds = memberIds|merge([entry.id]) %}
        {% endif %}
    {% endfor %}
{% endif %}

{% if memberIds|length %}
    {% set queryParams = queryParams|merge({
        id: memberIds
    })%}
{% endif %}
1

I wanted to post this in case someone can use a portion of it to help with their search queries. This is what we came up with, works in combination with our three other category dropdowns (all four can be multiple selections), plus some custom Javascript.

{% set categoriesArray = ['and'] %}

{# Institution #}
{% set searchInstitution = craft.request.getParam('institution') | replace({'|' : '\''}) %}
{% if searchInstitution is not empty %}
{% set institutions = craft.categories.search( searchInstitution ) %}
{% set categoriesArray = categoriesArray|merge([{targetElement: institutions, field: 'position.institution'}]) %}
{% endif %}

JS

var institutionNames = cbxInstitution.toArray()
      .reduce(function (p, c) {
        if (c.checked) p.push(c.value);
        return p;
      }, []);
    inInstitution.val(institutionNames.map(function(v){ return "|"+v.trim()+"|"; }).join(" OR "));
| improve this answer | |

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.