<p>In my Case i had to add .all() at the end then it worked with the Verison
&quot;Craft Solo 4.1.4.1&quot;</p>

CC BY-SA 4.0
2022-07-21T15:36:00.070Z
2022-07-21T15:36:00.070Z
Nik Lussy
<p>You can grab a specific block type in Matrix by doing this:</p>

<pre><code>{% for block in entry.myMatrixField.type('typeOne') %}
    {{ block.text | markdown }}
{% endfor %}
</code></pre>

<p>If you wanted to know the number of blocks of a certain type, you can do:</p>

<pre><code>{% set blocks = entry.myMatrixField.type('typeOne') %}
{{ blocks | length }}
</code></pre>

<p>If you want to search for multiple block types, you can do this:</p>

<pre><code>{% set blocks = entry.myMatrixField.type('typeOne, typeTwo') %}
</code></pre>

<p>For your specific example, it would look like this:</p>

<pre><code>{% set typeOneBlocks = entry.myMatrixField.type('typeOne') %}

{% if typeOneBlocks | length %}
    &lt;div class="divAWrapper"&gt;
        {% for block in typeOneBlocks %}
            &lt;div class="divA"&gt;{# do stuff #}&lt;/div&gt;
        {% endfor %}
    &lt;/div&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-07-10T19:59:16.403Z
6 minutes for a reply? Seriously, you guys are amazing. I'm not sure if I missed the documentation or if you just added it, but thanks for the quick help. http://buildwithcraft.com/docs/matrix-fields
idahotallpaul
CC BY-SA 3.0
2014-07-10T20:21:04.437Z
It's been in the docs for awhile. :)
Brad Bell
CC BY-SA 3.0
2015-01-08T14:02:17.747Z
@BradBell I 'd like to filter matrix block by category field. could you please guide for the same.
Bhashkar Yadav
CC BY-SA 3.0
2014-07-10T19:42:55.633Z
2014-07-10T19:42:55.633Z
Brad Bell
<p>Is there a way to filter a matrix by block type, or get a count of how many items there are of a certain type? Can't find a syntax example.</p>

<p>I created a matrix with two blocks. In pseudo code, basically what I want to do is this:</p>

<pre><code>{% set typeOneBlocks = theMatrix blocks where type == "typeOne" %}

{% if typeOneBlocks != null %}
    &lt;div class="divAWrapper"&gt;
        {% for block in typeOneBlocks %}
            &lt;div class="divA"&gt;{# do stuff #}&lt;/div&gt;
        {% endfor %}
    &lt;/div&gt;
{% endif %}
</code></pre>

<p>Same with <code>typeTwo</code> blocks. If there are no items of one type or the other, I don't want to create that container and have it sitting empty. </p>

<p>If there is not a way to filter or get a count, I'd have to use for loops to increment vars for how many of each block type there are and then act based on those counts. Seems there should be a simpler way.</p>

<templating><matrix><forloop>
How do you filter matrix blocks by type?
# How do you filter matrix blocks by type?

(Asked by: username_0 on 2014-07-10)

Is there a way to filter a matrix by block type, or get a count of how many items there are of a certain type? Can't find a syntax example.
I created a matrix with two blocks. In pseudo code, basically what I want to do is this:
{% set typeOneBlocks = theMatrix blocks where type == "typeOne" %}

{% if typeOneBlocks != null %}
    
        {% for block in typeOneBlocks %}
            {# do stuff #}
        {% endfor %}
    
{% endif %}

Same with typeTwo blocks. If there are no items of one type or the other, I don't want to create that container and have it sitting empty. 
If there is not a way to filter or get a count, I'd have to use for loops to increment vars for how many of each block type there are and then act based on those counts. Seems there should be a simpler way.

**Tags:** templating, matrix, forloop


---

## 2 answers

### Answer by username_2 on 2014-07-10. Score: 20 | Accepted answer

You can grab a specific block type in Matrix by doing this:
{% for block in entry.myMatrixField.type('typeOne') %}
    {{ block.text | markdown }}
{% endfor %}

If you wanted to know the number of blocks of a certain type, you can do:
{% set blocks = entry.myMatrixField.type('typeOne') %}
{{ blocks | length }}

If you want to search for multiple block types, you can do this:
{% set blocks = entry.myMatrixField.type('typeOne, typeTwo') %}

For your specific example, it would look like this:
{% set typeOneBlocks = entry.myMatrixField.type('typeOne') %}

{% if typeOneBlocks | length %}
    
        {% for block in typeOneBlocks %}
            {# do stuff #}
        {% endfor %}
    
{% endif %}

> **Comments:**
> **username_0**: 6 minutes for a reply? Seriously, you guys are amazing. I'm not sure if I missed the documentation or if you just added it, but thanks for the quick help. http://buildwithcraft.com/docs/matrix-fields  
> **username_2**: It's been in the docs for awhile. :)  
> **username_3**: @BradBell I 'd like to filter matrix block by category field. could you please guide for the same.  

---

### Answer by username_1 on 2022-07-21. Score: 1

In my Case i had to add .all() at the end then it worked with the Verison
"Craft Solo 4.1.4.1"


---

