2

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!

1

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

5
  • Thanks! Can I use this parameter in a entries query? – buckdanny May 2 at 16:06
  • I totally butchered that answer, didn't I? I updated it now to code that should actually work. I hope this helps! – Joorren May 2 at 22:33
  • looks great, but I cant get it working. It just get zero results. What to do? – buckdanny May 3 at 14:06
  • I tried the code without .type('video')and it is not working too. Maybe it is something with the relatedTo parameter? – buckdanny May 3 at 14:16
  • 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 May 3 at 14:25
0

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() %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.