2

I have several entry types, where I want to set a special element criteria for the entryTypeHandleX entry type, and simultaneously loop through all entry types included entryTypeHandleX.

The criteria I want to set is to skip first X entries for entryTypeHandleX.

In my template it looks like this:

{# Set ECM for all sections and entry types #}
{% set params = craft.entries.section().types() %} 

{# Set ECM for all channels and special entry type #}
{% set special = craft.entries.section().type('entryTypeHandleX').offset(X) %} 

{# Set ECM for entries. Check var 'params', Where the data are coming from!!! #}
{% set entries = params.limit(10) %}

{% paginate entries as entriesOnPage %}

{% for entry in entriesOnPage %}
{# entry #}
{% endfor %}

{% endpaginate %}
3

One way to merge two distinct elementCriteriaModels is to perform each query separately grabbing their 'ids', then merge the 'ids', and perform a new query using the 'ids' as the only parameter.

{% set first10 = craft.entries.type('entryTypeHandle').limit(10).ids() %}
{% set all = craft.entries.ids() %}
{% set first10-All = all|without(first10) %} 
{% set entryIds = craft.entries.limit(10).id(first10-All).ids() %}
{% set specialIds = craft.entries.type('entryTypeHandle').offset(10).ids() %}
{% set entryIDs = entryIds|merge(specialIds) %}

{% set entries = craft.entries.id(entryIds) %}
{% paginate entries as entriesOnPage %}
    {% for entry in entriesOnPage %}
        {# entry #}
    {% endfor %}
{% endpaginate %}

I also notice a few other issues in your example, that might be causing you some problems.

  • I see a lot of empty query parameters which are not needed — keep in mind that empty parameters will not really affect the results. (i.e. craft.entries.section().types() is really the same as just craft.entries, which, in this case, will return all entries).

  • By modifying an existing query, you are essentially creating two queries to the database, which may not be what you're after.

    {% set params = craft.entries.section().types() %}
    {% set entries = params.limit(10) %}
    

    This works, but you are essentially querying the database twice, creating two elementCriteriaModels. A better way to do this is to use a parameters array.

    {% set params = {
       section: 'mySectionHandle'
    } %}
    

    If you later need to add more parameters, you can do so using the merge filter, before running the query. The result is that you will only be querying the database once.

    {% set params = params|merge({ limit:10 }) %}
    {% set entries = craft.entries(params) %}
    
|improve this answer|||||
  • Performed it and I got a PHP notice - Array to string conversion. – Dominik Krulak May 9 '15 at 19:33
  • And thank you @Douglas McDonald for issues explanation. I'm aware of that. I actually used the code, that craft.entries.section().types() has variables defined. Sorry – Dominik Krulak May 9 '15 at 19:35

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.