Missing Matrix row types on output after upgrade to Craft 3 (subsetting and cloning ElementQueries) Update: Now I know the cause of my problem, I have appended to the original question title to try and help other people stumbling across the same issue.



I have a pending support ticket open with P&amp;T on this, but just wondering if anyone else has had a similar experience and might be able to point me in the right direction.

I have a development site running happily on Craft 2.6.3013. As a first foray into Craft 3 I thought I would upgrade it to Craft 3 in a dedicated folder and DB of my local setup.

All seemed to go fine. I worked through and eliminated deprecation errors in my 2 well developed templates. 

But one of the templates utilises a Matrix field with 2 block types -- subsection and item -- and only subsection rows are publishing.

The 20 rows originally in the field (in its singles entry) are all there in the back end, and their respective types show up as they should there: 5 for subsection and 15 for item.

But a simplified front end loop of: 

&lt;ul&gt;
    {% for row in entry.timeline.all() %}
        &lt;li&gt;{{ row.type }}&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;

…only produces the following outcome:

&lt;ul&gt;
    &lt;li&gt;subsection&lt;/li&gt;
    &lt;li&gt;subsection&lt;/li&gt;
    &lt;li&gt;subsection&lt;/li&gt;
    &lt;li&gt;subsection&lt;/li&gt;
    &lt;li&gt;subsection&lt;/li&gt;
&lt;/ul&gt;
Turns out my issue is because I am running 2 loops through entry.timeline: 


A subset of the field with only subsection blocks to generate a table of contents
The entire field contents with a mixture of both subsection and item block types.


Craft 2 was totally OK with me defining a subset and then looping through the entire set, but with Craft 3, if the subset is defined first, it seems to persist when the second loop runs, even if the scope of the second loop is defined differently.

So my current solution is to have a template structured like this:

{# Fetch the entire set first even though it will run second #}
{% set timelineItems = entry.timeline.all() %}

{# Fetch the subset second even though it will run first #}
{% set tocItems = entry.timeline.type('subsection').all() %}

{% for item in tocItems %}
    {# do subsetted ToC stuff #}
{% endfor %}

{% for row in timelineItems %}
    {# do entire timeline stuff #}
{% endfor %}


See https://github.com/craftcms/cms/issues/2160 for more background, and cloning element queries in the docs.