1

I'm building a site where the client has courses that run once a week. We'll have several courses and each course will have a matrix with multiple blocks detailing that weeks class.

What I want to do is output the events in either a calendar (Full Calendar) or a list of cards ordered by date of the weekly event.

Something like this Where basketweaving and cooking indian curry are entries and each week is a matrix block within the entry.

  • July 1, 2023 - Basket weaving -An introduction (week 1)
  • July 3, 2023 - Cooking Indian Curry - different types (week 1)
  • July 7, 2023 - Basket weaving - making your first basket (week 2)
  • July 8, 2023 - Cooking Indian Curry - your first Vindaloo (week 2)
  • July 14, 2023 - Basket weaving - Intermediate techniques (week 3)
  • July 16, 2023 - Cooking Indian Curry - taming the spice (week 3)

Is it possible to output and mix matrix blocks across entries?

1 Answer 1

2

You can use a Matrix Block Query to query blocks directly, without having to go through the entries. This allows you to get an array of matrix blocks ordered by date, regardless of what entries they belong to. You can limit the query to a specific type or to blocks on a specific field to prevent other matrix fields from appearing in the results. Assuming a block type schedule and a date field week_start on the matrix blocks, the query can look like this:

{% set schedule = craft.matrixBlocks()
    .type('schedule')
    .orderBy('week_start ASC')
    .all()
%}

If you need to limit the calendar to specific matrix blocks on specific entries (for example, to build a filter interface), you can use the owner parameter on the query:

{% set courses = craft.entries()
    .section('courses')
    .title(['Basket weaving', 'Cooking Indian Curry'])
    .ids()
%}

{% set schedule = craft.matrixBlocks()
    .type('schedule')
    .orderBy('week_start ASC')
    .owner(courses)
    .all()
%}
1
  • 1
    Thank you that helped immensely - got everything working as expected
    – CreateSean
    Commented Jun 6, 2023 at 14:15

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.