Issue with Matrix `type` property and Live Preview? I have problems getting Live Preview to work with this Twig code that gets data from a Matrix field.

{{ entry.matrixField.type('copy').nth(0).plainText }}


As soon as I remove the type property from the criteria model, it does update on entry changes again!
Live preview have gone through a major refactoring for Craft 3.2, which addresses this issue. For Craft 3.1 and below see solutions below.



This is happening because Matrix will only show the non-live blocks so long as you don’t set any Matrix parameters in your template. As soon as you tell it to only show blocks of a certain type, it no longer feels confident that the blocks it was going to output are the right ones, so it will dump them and run a new DB query.

One possible way around this would be to do this:

Craft 2:

{% if craft.request.isLivePreview %}
    {% set firstCopyBlock = null %}
    {% for block in entry.matrixField %}
        {% if firstCopyBlock == null and block.type.handle == 'copy' %}
            {% set firstCopyBlock = block %}
        {% endif %}
    {% endfor %}
{% else %}
    {% set firstCopyBlock = entry.matrixField.type('copy').first() %}
{% endif %}

{% if firstCopyBlock %}
    {{ firstCopyBlock.plainText }}
{% endif %}


Craft 3:

{% if craft.app.request.isLivePreview %}
    {% set firstCopyBlock = null %}
    {% for block in entry.matrixField.all() %}
        {% if firstCopyBlock == null and block.type.handle == 'copy' %}
            {% set firstCopyBlock = block %}
        {% endif %}
    {% endfor %}
{% else %}
    {% set firstCopyBlock = entry.matrixField.type('copy').one() %}
{% endif %}

{% if firstCopyBlock %}
    {{ firstCopyBlock.plainText }}
{% endif %}