I'm struggling to get an element criteria model of more matrix fields at once for entry related to tagField.

Example: I have two matrixFields, where I want them in {% for block in entry.matrixFieldhandle %} loop.

I could do

{% for block in entry.matrixFieldhandle1 %} My stuff {% endfor %}


{% for block in entry.matrixFieldhandle2 %} My stuff {% endfor %}

but it's kind of silly to always set element model for each field. Anything simpler?

Couldn't find anything explanatory in documentation or here.

  • I'm not sure I follow... Wouldn't you just output them back-to-back? Don't your matrix fields contain different block types, and therefore have different requirements for being displayed? – Lindsey D Feb 26 '15 at 20:27
  • Question updated. I have the field's structure strictly set to met my needs for site. Matrix field(set according to categories) -> Block type -> Field – Dominik Krulak Feb 26 '15 at 20:54
  • Instead of creating multiple matrix fields and trying to merge them, I would suggest simply creating a new 'block type' within the same matrix with different fields defined. That is the beauty of matrix fields! – Douglas McDonald Feb 27 '15 at 4:59

Not sure if this solves your problem, but you could try this:

{% set allMatrixBlocks = entry.matrixFieldhandle1.find | merge(entry.matrixFieldhandle2.find) %}

{% for block in allMatrixBlocks %} My stuff {% endfor %}
  • It didn't work, because it isn't array. – Dominik Krulak Feb 27 '15 at 13:13
  • Sorry, updated my answer to include .find. That will convert each field into an array of matrix blocks. In some cases, Craft will automatically convert an ElementCriteriaModel into an array if it's being treated as an array... I wasn't sure if this was one of those situations (apparently it isn't). – Lindsey D Feb 27 '15 at 15:24
  • It works as it should! :) – Dominik Krulak Feb 27 '15 at 20:20

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.