<p>You could loop through the Matrix field on every entry save using the awesome Preparse plugin, to store the categories an entry contains in a new field.</p>

<p><a href="https://github.com/aelvan/Preparse-Field-Craft" rel="nofollow noreferrer">https://github.com/aelvan/Preparse-Field-Craft</a></p>

<p>Here’s example code for your Preparse field, it would store strings like <code>firstCategory,thirdCategory,secondCategory</code>.</p>

<pre><code>{%- set categories = [] %}

{%- for block in entry.myMatrixField.type('myRelevantBlockType') %}
    {%- if block.myDropdownField %}
        {%- set categories = categories|merge([ block.myDropdownField.value ]) %}
    {%- endif %}
{%- endfor %}

{{- categories|join(',') -}}
</code></pre>

<p>Once you have populated that field for all your entries, you can filter them by a category using this criteria model.</p>

<pre><code>{% set categoryToSearch = 'secondCategory' %}

{% set entries = craft.entries({
    section: 'myEntriesSection',
    search: 'myPreparseFieldHandle:*' ~ categoryToSearch ~ '*',
}) %}
</code></pre>

<p>I have to say that I’m not very proud of this solution as it uses <code>search</code> which I usually try to avoid. But I wasn’t able to come up with a better idea.</p>

CC BY-SA 3.0
2016-11-16T15:02:00.837Z
How would this work with existing imported content? I have imported over 65K + entries and about 7K entries need that.
Subrato M
CC BY-SA 3.0
2016-11-16T16:23:44.597Z
Disable `devMode`, then go to Settings > Section > My Section where you click "Save" (you don’t have to edit any section settings). This should trigger a "Re-save Elements" task which should update all Preparse fields as well. 65K entries will take a while!
carlcs
CC BY-SA 3.0
2016-11-17T20:16:24.533Z
Thanks @carlcs I am going to give it a try and hopefully, it takes care of this easily
Subrato M
CC BY-SA 3.0
2016-12-01T16:38:20.243Z
I haven't really worked on onSave events, can you expand on it? Also sorry been busy with other stuff so craft was on backburner. I am looking at ParseFieldPlugin.php. Do I assign categories | join(',') to new text field?
Subrato M
CC BY-SA 3.0
2016-12-01T17:23:42.270Z
So I am not sure what I did wrong. I installed the plugin, created a field of that type, added it to my section, added the code you as an example and saved it. The field doesn't have any value. Is there a way to debug and see if what I did was right or wrong? Also ignore my previous comment
Subrato M
CC BY-SA 3.0
2016-11-16T09:12:59.527Z
2016-11-16T09:12:59.527Z
carlcs
<p>I have a matrix inside of which I have 2 fields, 1. dropdown list containing download types and another asset field. I would like to search first by a category and then I would like to further filter them and see if they have downloads of a certain type. </p>

<p>I would like to avoid a 2 step searching if possible and search articles that belong to certain category that have a certain download type associated with it.</p>

<p>Any idea on how to achieve this?</p>

<matrix><categories>
Search entries by matrix field
# Search entries by matrix field

(Asked by: username_0 on 2016-11-16)

I have a matrix inside of which I have 2 fields, 1. dropdown list containing download types and another asset field. I would like to search first by a category and then I would like to further filter them and see if they have downloads of a certain type.

I would like to avoid a 2 step searching if possible and search articles that belong to certain category that have a certain download type associated with it.

Any idea on how to achieve this?

**Tags:** matrix, categories


---

## 1 answers

### Answer by username_1 on 2016-11-16. Score: 3 | Accepted answer

You could loop through the Matrix field on every entry save using the awesome Preparse plugin, to store the categories an entry contains in a new field.
https://github.com/aelvan/Preparse-Field-Craft
Here’s example code for your Preparse field, it would store strings like firstCategory,thirdCategory,secondCategory.
{%- set categories = [] %}

{%- for block in entry.myMatrixField.type('myRelevantBlockType') %}
    {%- if block.myDropdownField %}
        {%- set categories = categories|merge([ block.myDropdownField.value ]) %}
    {%- endif %}
{%- endfor %}

{{- categories|join(',') -}}

Once you have populated that field for all your entries, you can filter them by a category using this criteria model.
{% set categoryToSearch = 'secondCategory' %}

{% set entries = craft.entries({
    section: 'myEntriesSection',
    search: 'myPreparseFieldHandle:*' ~ categoryToSearch ~ '*',
}) %}

I have to say that I’m not very proud of this solution as it uses search which I usually try to avoid. But I wasn’t able to come up with a better idea.

> **Comments:**
> **username_0**: How would this work with existing imported content? I have imported over 65K + entries and about 7K entries need that.  
> **username_1**: Disable `devMode`, then go to Settings > Section > My Section where you click "Save" (you don’t have to edit any section settings). This should trigger a "Re-save Elements" task which should update all Preparse fields as well. 65K entries will take a while!  
> **username_0**: Thanks @username_1 I am going to give it a try and hopefully, it takes care of this easily  
> **username_0**: I haven't really worked on onSave events, can you expand on it? Also sorry been busy with other stuff so craft was on backburner. I am looking at ParseFieldPlugin.php. Do I assign categories | join(',') to new text field?  
> **username_0**: So I am not sure what I did wrong. I installed the plugin, created a field of that type, added it to my section, added the code you as an example and saved it. The field doesn't have any value. Is there a way to debug and see if what I did was right or wrong? Also ignore my previous comment  

---

