Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-20T06:37:24.013Z
Answers.LastActivityDate: 2022-05-20T06:37:24.013Z
Answers.UserDisplayName: Ben Croker
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-02T16:06:39.400Z
Answers.Text: Thanks! Can I use this parameter in a entries query?
Answers.UserDisplayName: buckdanny
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-02T22:33:50.763Z
Answers.Text: I totally butchered that answer, didn't I? I updated it now to code that *should* **actually** work. I hope this helps!
Answers.UserDisplayName: Joorren
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-03T14:06:57.757Z
Answers.Text: looks great, but I cant get it working. It just get zero results. What to do?
Answers.UserDisplayName: buckdanny
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-03T14:16:38.633Z
Answers.Text: I tried the code without  ````.type('video')````and it is not working too. Maybe it is something with the relatedTo parameter?
Answers.UserDisplayName: buckdanny
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-03T14:25:41.400Z
Answers.Text: 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
Answers.UserDisplayName: buckdanny
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-01T21:09:07.353Z
Answers.LastActivityDate: 2021-05-02T22:32:30.207Z
Answers.LastEditDate: 2021-05-02T22:32:30.207Z
Answers.UserDisplayName: Joorren
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-04T20:36:02.133Z
Answers.LastActivityDate: 2021-05-04T20:36:02.133Z
Answers.UserDisplayName: buckdanny
Body: <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>
  
Tags: <entries>
Title: Filter Entries by Matrix Block
ThreadText: # 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.
  
  1. Query all of the matrix blocks that you care about, fetching their IDs.
  2. 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](https://craftcms.com/docs/3.x/relations.html)
  
  > **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() %}
  
  ```
  
  
  ---
  
  