<p>This answer works in both Craft 3 and 4, and is as readable and performant as possible.</p>
<ol>
<li>Query all of the matrix blocks that you care about, fetching their IDs.</li>
<li>Fetch the entries that contain those matrix block IDs.</li>
</ol>
<pre><code>{% set matrixBlockIds = craft.matrixBlocks()
    .type('video')
    .ids() %}

{% set entries = craft.entries()
    .section(['events', 'sessions'])
    .orderBy('datum desc')
    .matrixField(matrixBlockIds)
    .limit(30)
    .all() %}
</code></pre>
<p>References:<br />
<a href="https://craftcms.com/docs/4.x/matrix-blocks.html#querying-matrix-blocks" rel="nofollow noreferrer">https://craftcms.com/docs/4.x/matrix-blocks.html#querying-matrix-blocks</a><br />
<a href="https://craftcms.com/docs/4.x/matrix-fields.html#querying-elements-with-matrix-fields" rel="nofollow noreferrer">https://craftcms.com/docs/4.x/matrix-fields.html#querying-elements-with-matrix-fields</a></p>

CC BY-SA 4.0
2022-05-20T06:37:24.013Z
2022-05-20T06:37:24.013Z
Ben Croker
<p>You'll have to query all matrixes in <em>that</em> <code>field</code> with <em>that</em> <code>type</code> first. With these results, you should then be able to query the entries that are <code>relatedTo</code> these results.</p>
<p>You'll end up with</p>
<pre><code>{% set matrixes = craft.matrixBlocks()
    .field('matrixField')
    .type('video')
    .all() %}

{% set entries = craft.entries()
    .section(['events', 'sessions'])
    .orderBy('datum desc')
    .matrixField(':notempty:')
    .relatedTo(matrixes)
    .limit(30)
    .all() %}
</code></pre>
<p>Documentation about relations can be found <a href="https://craftcms.com/docs/3.x/relations.html" rel="nofollow noreferrer">here</a></p>

CC BY-SA 4.0
2021-05-02T16:06:39.400Z
Thanks! Can I use this parameter in a entries query?
buckdanny
CC BY-SA 4.0
2021-05-02T22:33:50.763Z
I totally butchered that answer, didn't I? I updated it now to code that *should* **actually** work. I hope this helps!
Joorren
CC BY-SA 4.0
2021-05-03T14:06:57.757Z
looks great, but I cant get it working. It just get zero results. What to do?
buckdanny
CC BY-SA 4.0
2021-05-03T14:16:38.633Z
I tried the code without  ````.type('video')````and it is not working too. Maybe it is something with the relatedTo parameter?
buckdanny
CC BY-SA 4.0
2021-05-03T14:25:41.400Z
And if if do ````{{ dump(matrixes) }}```` I get an object from the matrix that seems to be right. Now the question is how to work with this object in the relationship parameter for the entries query
buckdanny
CC BY-SA 4.0
2021-05-01T21:09:07.353Z
2021-05-02T22:32:30.207Z
2021-05-02T22:32:30.207Z
Joorren
<p>based on this: <a href="https://craftcms.stackexchange.com/a/30799/11762">https://craftcms.stackexchange.com/a/30799/11762</a></p>
<p>I figure out a way how to do this, but maybe where is a better/cleaner way how to filter an entries query by its matrix fields?</p>
<p>Now I use the <code>id</code> parameter to filter the entries query:</p>
<pre><code>{% set matrixes = craft.matrixBlocks()
    .field('matrixFieldName')
    .type('sound')
    .all() %}

{# Get entry IDs #}
{% set id = [] %}
{% for block in matrixes %}
     {% set id = id|merge([block.owner.id]) %}
{% endfor %}


{% set entries = craft.entries()
    .section(['events', 'sessions'])
    .id(id)
    .limit(30)
    .all() %}
</code></pre>

CC BY-SA 4.0
2021-05-04T20:36:02.133Z
2021-05-04T20:36:02.133Z
buckdanny
<p>I have a matrix field with two block-types (&quot;sound&quot; and &quot;video&quot;)</p>
<p>I want to get all entries that have a matrix field with a certain block-type.</p>
<p>At the moment I have:</p>
<pre><code>{% set entries = craft.entries()
        .section(['events', 'sessions'])
        .orderBy('datum desc')
        .matrixField(':notempty:')
        .limit(30)
        .all() %}
</code></pre>
<p>how can i get all entries that have a &quot;video&quot;-block in the matrix-field?</p>
<p>I want to paginate the results.</p>
<p>Thanks!</p>

<entries>
Filter Entries by Matrix Block
# Filter Entries by Matrix Block

(Asked by: username_0 on 2021-05-01)

I have a matrix field with two block-types ("sound" and "video")
I want to get all entries that have a matrix field with a certain block-type.
At the moment I have:
{% set entries = craft.entries()
        .section(['events', 'sessions'])
        .orderBy('datum desc')
        .matrixField(':notempty:')
        .limit(30)
        .all() %}

how can i get all entries that have a "video"-block in the matrix-field?
I want to paginate the results.
Thanks!

**Tags:** entries


---

## 3 answers

### Answer by username_1 on 2022-05-20. Score: 3

This answer works in both Craft 3 and 4, and is as readable and performant as possible.

Query all of the matrix blocks that you care about, fetching their IDs.
Fetch the entries that contain those matrix block IDs.

{% set matrixBlockIds = craft.matrixBlocks()
    .type('video')
    .ids() %}

{% set entries = craft.entries()
    .section(['events', 'sessions'])
    .orderBy('datum desc')
    .matrixField(matrixBlockIds)
    .limit(30)
    .all() %}

References:
https://craftcms.com/docs/4.x/matrix-blocks.html#querying-matrix-blocks
https://craftcms.com/docs/4.x/matrix-fields.html#querying-elements-with-matrix-fields


---

### Answer by username_2 on 2021-05-01. Score: 2

You'll have to query all matrixes in that field with that type first. With these results, you should then be able to query the entries that are relatedTo these results.
You'll end up with
{% set matrixes = craft.matrixBlocks()
    .field('matrixField')
    .type('video')
    .all() %}

{% set entries = craft.entries()
    .section(['events', 'sessions'])
    .orderBy('datum desc')
    .matrixField(':notempty:')
    .relatedTo(matrixes)
    .limit(30)
    .all() %}

Documentation about relations can be found here

> **Comments:**
> **username_0**: Thanks! Can I use this parameter in a entries query?  
> **username_2**: I totally butchered that answer, didn't I? I updated it now to code that *should* **actually** work. I hope this helps!  
> **username_0**: looks great, but I cant get it working. It just get zero results. What to do?  
> **username_0**: I tried the code without  ````.type('video')````and it is not working too. Maybe it is something with the relatedTo parameter?  
> **username_0**: And if if do ````{{ dump(matrixes) }}```` I get an object from the matrix that seems to be right. Now the question is how to work with this object in the relationship parameter for the entries query  

---

### Answer by username_0 on 2021-05-04. Score: 1

based on this: https://craftcms.stackexchange.com/a/30799/11762
I figure out a way how to do this, but maybe where is a better/cleaner way how to filter an entries query by its matrix fields?
Now I use the id parameter to filter the entries query:
{% set matrixes = craft.matrixBlocks()
    .field('matrixFieldName')
    .type('sound')
    .all() %}

{# Get entry IDs #}
{% set id = [] %}
{% for block in matrixes %}
     {% set id = id|merge([block.owner.id]) %}
{% endfor %}


{% set entries = craft.entries()
    .section(['events', 'sessions'])
    .id(id)
    .limit(30)
    .all() %}


---

