1

I have Entries with a Matrix field called events. I have an Entries field and a Dropdown field inside those Matrix blocks (block type event). I'm currently filtering by entry using andRelatedTo(['and', {targetElement: entry.id, field: 'events.entry'}]).

I would also like to add the ability to filter via the dropdown, but I'm unsure of how I can target that dropdown:

{% set eventsQuery = craft.entries({
  section: 'events',
  events.event.dropdown: 'categoryOne'
}) %}

## I'm doing this with Sprig, so adding the relations this way
{% do eventsQuery.andRelatedTo(['and', {targetElement: entry.id, field: events.entry}]) %}

events.event.dropdown: 'categoryOne' is how it would work in my mind, but obviously is incorrect.

Can anyone show me how I could add the dropdown inside a Matrix block as a filter? Thanks!

1 Answer 1

3

You'll need to do a matrixBlocks query first to get the IDs of all the relevant matrix blocks, then your entries query referencing those ids:

{% set blockIds = craft.matrixBlocks.field('events').type('event').dropdown('categoryOne').ids() %}
{% set eventsQuery = craft.entries.section('events').events(blockIds) %}

(Or here's the same thing if you really prefer the object syntax)...

{% set blockIds = craft.matrixBlocks({
    field: 'events',
    type: 'event',
    dropdown: 'categoryOne'
}).ids() %}
    
{% set eventsQuery = craft.entries({
    section: 'events',
    events: blockIds
}) %}

https://craftcms.com/docs/4.x/matrix-blocks.html#querying-matrix-blocks

3
  • Thanks for the answer! I'm not sure if it'll work with the way I'm filtering stuff in Sprig, but I'll see how I go.
    – bigdev
    Commented Mar 3, 2023 at 12:54
  • So I think this is my issue {% set blockIds = craft.matrixBlocks.type('event').dropdown('categoryOne').ids() is returning the same number of items no matter the category set. It doesn't seem to filter them that way.
    – bigdev
    Commented Mar 3, 2023 at 13:01
  • 1
    Looks like you just need to specify the Matrix field too: {% set blockIds = craft.matrixBlocks.field('events').type('event').dropdown('categoryOne').ids() %} Bingo! Thanks!
    – bigdev
    Commented Mar 3, 2023 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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