<p>As far as I know, (currently) the only way to filter matrix blocks is to use the <a href="http://buildwithcraft.com/docs/matrix-fields#filtering-by-block-type" rel="nofollow"><code>type</code></a> method to filter by block type or to limit the amount of returned matrix models with <a href="http://buildwithcraft.com/docs/matrix-fields#adjusting-the-limit" rel="nofollow"><code>limit</code></a>.</p>

<p>Here's what I'd do:</p>

<pre><code>{% set searchString = 'sitelogo' %}

{# Search for potential matrix blocks #}
{% set foundBlocks = attachmentGlobals.globalFiles.type('files').search(searchString) %}

{# Loop through the result and output the right one #}
{% for foundBlock in foundBlocks %}

    {% if foundBlock.fileId == searchString %}
        &lt;img src="{{ foundBlock.fileAsset.first.url }}" alt="{{ foundBlock.fileAsset.first.title }}"&gt;
    {% endif %}

{% endfor %}
</code></pre>

<p>You can't use <code>search('fileId:sitelogo')</code>, because that would filter for entries with  <code>fileId:sitelogo</code> and not entries with a matrix block <code>fileId:sitelogo</code>.</p>

CC BY-SA 3.0
2014-11-13T08:41:10.137Z
How would you do that in a plugin?
David A McInnis
CC BY-SA 3.0
2014-11-13T08:50:21.960Z
@David sorry, can't help you with a plugin solution. I've still no experience with php5 (yet)! Have a look at the [methods available for MatrixBlockModel](https://buildwithcraft.com/classreference/models/MatrixBlockModel#public-methods), you should be able to do the exact same conditional in your plugin. If you need further help, better ask a new question because I don't think many will notice your comment.
carlcs
CC BY-SA 3.0
2014-08-22T12:07:59.200Z
2014-08-22T15:04:04.340Z
2014-08-22T15:04:04.340Z
carlcs
<p>I've set up a global set "attachmentGlobals" with a matrix field inside named "globalFiles". This matrix has only one block "files" which has a text field "fileId" and an Asset field "fileAsset".</p>

<p>I would like to retrieve a specific row searching for the "fileId" content.</p>

<p>I've triyed in those ways:</p>

<p><strong>Test 1:</strong></p>

<pre><code>attachmentGlobals.globalFiles.fileId('sitelogo')
</code></pre>

<p>Return me an Error saying no method "fileId" is available</p>

<p><strong>Test 2:</strong></p>

<pre><code>attachmentGlobals.globalFiles.find('fileId:sitelogo')
</code></pre>

<p>No error, but no results</p>

<p><strong>Test 3:</strong></p>

<pre><code>attachmentGlobals.globalFiles.search('fileId:sitelogo')
</code></pre>

<p>No error, but no results</p>

<p>I've search Craft documentation and some examples on Stack Exchange, but nothing seems to help.</p>

<p>In the first place, is this possible? or were am I wrong?</p>

<p>Thank you in advance.</p>

<p><em>PS: I know this issue could be solved using only assets fields, but I would like to know if is possible to search for matrix fields by filed content.</em></p>

<matrix><search><globals>
Search for matrix field in a global set
# Search for matrix field in a global set

(Asked by: username_0 on 2014-08-22)

I've set up a global set "attachmentGlobals" with a matrix field inside named "globalFiles". This matrix has only one block "files" which has a text field "fileId" and an Asset field "fileAsset".
I would like to retrieve a specific row searching for the "fileId" content.
I've triyed in those ways:
Test 1:
attachmentGlobals.globalFiles.fileId('sitelogo')

Return me an Error saying no method "fileId" is available
Test 2:
attachmentGlobals.globalFiles.find('fileId:sitelogo')

No error, but no results
Test 3:
attachmentGlobals.globalFiles.search('fileId:sitelogo')

No error, but no results
I've search Craft documentation and some examples on Stack Exchange, but nothing seems to help.
In the first place, is this possible? or were am I wrong?
Thank you in advance.
PS: I know this issue could be solved using only assets fields, but I would like to know if is possible to search for matrix fields by filed content.

**Tags:** matrix, search, globals


---

## 1 answers

### Answer by username_1 on 2014-08-22. Score: 1

As far as I know, (currently) the only way to filter matrix blocks is to use the type method to filter by block type or to limit the amount of returned matrix models with limit.
Here's what I'd do:
{% set searchString = 'sitelogo' %}

{# Search for potential matrix blocks #}
{% set foundBlocks = attachmentGlobals.globalFiles.type('files').search(searchString) %}

{# Loop through the result and output the right one #}
{% for foundBlock in foundBlocks %}

    {% if foundBlock.fileId == searchString %}
        
    {% endif %}

{% endfor %}

You can't use search('fileId:sitelogo'), because that would filter for entries with  fileId:sitelogo and not entries with a matrix block fileId:sitelogo.

> **Comments:**
> **username_2**: How would you do that in a plugin?  
> **username_1**: @David sorry, can't help you with a plugin solution. I've still no experience with php5 (yet)! Have a look at the [methods available for MatrixBlockModel](https://buildwithcraft.com/classreference/models/MatrixBlockModel#public-methods), you should be able to do the exact same conditional in your plugin. If you need further help, better ask a new question because I don't think many will notice your comment.  

---

