6

So let's say I have the following code. I have the data to retrieve the entry I want, But I only want to return some of the matrix rows based on some criteria.

Example Code:

{% set entryId = 1 %}
{% set entry = craft.entries({ section: "mySection", id: entryId }).first() %}

{% set myMatrixOutput = entry.myMatrixField.find({ someFieldInTheMatix: 2 }) %}

{% for row in myMatrixOutput %}
    {{ row.someFieldInTheMatrix }}
{% endfor %}

I can't get that to work it simply return me everything. I know that I could adjust my loop to be

{% for row in myMatrixOutput if row.someFieldInTheMatix == 2 %}

But say I have a large number of rows in the matrix field it would seem a waste to do all that looping if it could just bring back the correct results?

  • You example code should work with Craft 2.3 :D – carlcs Dec 3 '14 at 15:41
3

As far as I know, the only criteria to filter Matrix blocks is the type property or to pick a block by it's position with first, last or the new nth filter.

Yes, it would totally make sense to have more options to grab select Matrix blocks and I am rather confident that this is s.th. that is already on "The List".

Update:

Craft 2.3 change things! Besides using the type property or to pick a block by it's position with first, last or the nth filter it is now possible to query for Matrix blocks using all sorts of custom field parameters.

{% set entry = craft.entries.slug('myMatrixEntry').first() %}

{# Query Matrix blocks using custom field parameters #}
{% set blocks = entry.myMatrixField.type('text').myLightswitchField('1').myDateField('> 2014-12-02') %}

{% for block in blocks %}
...
{% endfor %}

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.