Cms: Unexpected behavior with assigning element queries to variable

Created on 23 Feb 2021  路  2Comments  路  Source: craftcms/cms

Description

This is new to me and I have never met with it if the query I build has the expected behaviour .

I have about 2000 entries in section 'x' in 2 sites and I paginate them.

Here is the query:

{% set query = craft.entries().siteId(siteIds).section('x').status(null) %}

{% if query|length %}
    {% paginate query as pageInfo, matchedEntries %}
    {% for e in matchedEntries %}
        ....
    {% endfor %}
{% endif %}

That gives me expected result of about 2000 entries.

But when I try this:

{% set query = craft.entries().siteId(siteIds).section('x').status(null) %}
{% set someStats = query.after('2021-02-01').limit(null).all() %}

{% if query|length %}
    {% paginate query as pageInfo, matchedEntries %}
    {% for e in matchedEntries %}
        ....
    {% endfor %}
{% endif %}

I get unexpected result of entries dated after the date.
Either I don't see something that I'm doing wrong or there is something wrong.

Additional info

  • Craft version: 3.6.6
  • PHP version: 7.4.2
  • Database driver & version: MySQL 5.7.26
question

All 2 comments

This is the expected behavior as per https://craftcms.com/docs/3.x/upgrade.html#cloning-element-queries

You should be doing {% set someStats = clone(query).after('2021-02-01').limit(null).all() %} instead and everything should be working as you expect.

Ohh glad to know it! Thanks @andris-sevcenko

Was this page helpful?
0 / 5 - 0 ratings