<p>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. </p>

<pre><code>{% 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 %}
</code></pre>

<p>JS</p>

<pre><code>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 "));
</code></pre>

CC BY-SA 3.0
2017-06-26T23:12:38.443Z
2017-06-26T23:12:38.443Z
GrettyGoose
<p>I have a dropdown search query I have implemented using this blog (<a href="https://www.webstoemp.com/blog/combined-searches-and-filters-craft-cms" rel="nofollow noreferrer">https://www.webstoemp.com/blog/combined-searches-and-filters-craft-cms</a>)</p>

<p>This works excellent for searching my three categories and it's very fast.</p>

<pre><code>{{% 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 %}}
</code></pre>

<p>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! </p>

<pre><code>{% 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 %}
</code></pre>

<matrix>
Accesing craft.categories.slug ids within a MatrixBlock for search query
# Accesing craft.categories.slug ids within a MatrixBlock for search query

(Asked by: username_0 on 2017-06-26)

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

**Tags:** matrix


---

## 1 answers

### Answer by username_0 on 2017-06-26. Score: 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 "));


---

