I'm loading multiple matrix blocks from multiple entries to the homepage if the date is today or in the future. So far this works but now I'm trying to sort the blocks by date and limit the output to 10 dates.

Here's the code I'm currently using:

{% for entry in craft.entries.section('projects') %}

  {% for block in entry.agendav2 %}

    {% if block.Datum >= now %}

      {% switch block.type %}

        {% case 'Datum' %}

          <table class="agenda_item">
              <td class="table_title performance-date table_col-2-3">{{ block.Datum|date("d.m.Y") }}</th>
                <td class="table_title event-location table_col-1-3">{{ block.locatie }}</th>
              <td class="performance-title table_col-2-3"><a href="{{ entry.url }}">{{ entry.globalTitle }}</a></td>
              <td class="event-title table_col-1-3"><a target="_blank" href="{{ block.eventUrl }}">{{ block.evenement }}</a></td>

      {% endswitch %}

    {% endif %}   

  {% endfor %}  

{% endfor %}

Right now it's loading every upcoming date per entry like:

  • Entry 1: 31-01-2018
  • Entry 1: 01-02-2018
  • Entry 1: 02-03-2018
  • Entry 2: 30-01-2018
  • Entry 2: 06-02-2018
  • Entry 2: 03-03-2018
  • Entry 3: 23-01-2018
  • Entry 3: 25-01-2018
  • Entry 3: 08-03-2018

Your entry.agendav2 is not an array of elements but a ElementCriteriaModel so you can include both properties in your query

{% for block in entry.agendav2.limit(10).order('Datum') %}

That's basically it.

Edit: you can search for entries in the future with

 Datum('>= ' ~ 'now'|date('c')) 

Like stated here https://craftcms.stackexchange.com/a/23697/5557


After we discussed the issue in my comments I came up with this solution

{% set matrixBlocks = craft.yourPluginHandle.getSortedMatrixBlocks() %}
{% for matrix in matrixBlocks %}
    {{ matrix.datum }}
{% endfor %}

And insert this in your PluginVariable

public function getSortedMatrixBlocks(){
    // the id of your matrix field
    $matrixFieldId = 61;

    // check your database for your matrix field table and insert your Datum field column
    // it will be a string like field_{blockHandle}_{fieldHandle}
    // for me it is
    $matrixFieldDatum = 'field_test_datum';

    // lowercase handle of your field
    $matrixFieldHandle = 'matrixfield';
    $matrixIds = craft()->db->createCommand()
        ->where('craft_matrixblocks.fieldId = :fieldId', ['fieldId' => $matrixFieldId])
        ->join('matrixcontent_'.$matrixFieldHandle, 'craft_matrixblocks.id = craft_matrixcontent_' . $matrixFieldHandle . '.elementId')
        ->order($matrixFieldDatum . ' ASC')

    $allMatrixBlocks = [];
    foreach ($matrixIds as $id){
        $allMatrixBlocks[] = craft()->matrix->getBlockById($id);

    return $allMatrixBlocks;
  • Tried this but still it only sorts it per entry instead of mixing up the entries per date. This is what I have right now: sandmandance.com/dev Feb 1 '18 at 10:03
  • what do you mean with "It sorts it per entry"? You have to make sure you selected the correct field with the correct handle, I just used the code you provided Feb 1 '18 at 10:16
  • Ok, let me try to explain myself a little bit more: I've got a (channel)section named 'Projects' which has multiple entries. In those entries you can add multiple tour dates to the particular entry using a Matrix Field. This will result in a complete tour schedule per entry. Now I'd like to combine all these upcoming tour dates for all these entries and display the upcoming tour dates on the homepage in chronological order. Only the problem is, right now it finds the first entry, sort the upcoming dates of that particular entry and moves on to the next one. Feb 6 '18 at 10:00
  • Alright I see it now. Do you already have a project plugin or do you prefer to do everything in twig? Feb 6 '18 at 10:10
  • As for now I was trying to do it all in twig, but if there's a easy plugin solution for it I'm more than willing to give that a shot. Feb 6 '18 at 10:23

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.