<p>As <a href="https://craftcms.stackexchange.com/a/782/57">Victor rightfully points out</a>, the {% paginate %} tags require an ElementCriteriaModel, so you can't do what you're doing in your first example because as soon as you call:</p>

<pre><code>{% for databyte in allDatabytes %}
</code></pre>

<p>Craft hits the database and converts the ElementCriteriaModel into actual EntryModels.</p>

<p>We also don't have support, yet, for passing <code>:empty:</code> into things like AssetFields, TagFields, etc., which is why your second example won't work...  however that is on our list to add!</p>

<p>In this case, I think your best bet is going to be to do through the search service with something like this:</p>

<pre><code>{# Search for 10 entries in the 'databytes' section where the 'series' assetField doesn't have anything selected. #}
{% set myCriteria = craft.entries.section('databytes').search('-series:*').limit(10) %}

{% paginate myCriteria as entries %}
    {% for entry in entries %}
        {{ entry.title }}
    {% endfor %}

    {% if paginate.prevUrl %}
        &lt;a href="{{ paginate.prevUrl }}"&gt;Previous Page&lt;/a&gt;
    {% endif %}

    {% if paginate.nextUrl %}
        &lt;a href="{{ paginate.nextUrl }}"&gt;Next Page&lt;/a&gt;
    {% endif %}
{% endpaginate %}
</code></pre>

CC BY-SA 3.0
2014-07-09T04:59:12.290Z
Hi Brad, thank you that worked great. Its good to see there is so many different ways to attack a problem.
Gareth
CC BY-SA 3.0
2014-07-08T22:45:38.210Z
2014-07-08T22:45:38.210Z
2017-04-13T12:51:00.253Z
Brad Bell
<p>This won't work with your example, because the <a href="http://buildwithcraft.com/docs/templating/tags#paginate" rel="nofollow noreferrer">pagination-tag</a> only accepts an <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel" rel="nofollow noreferrer">ElementCriteriaModel</a>, not an array of already fetched entries:</p>

<pre class="lang-twig prettyprint-override"><code>{% paginate MyCriteria as PageSubset %}
</code></pre>

<p>See also: <a href="https://craftcms.stackexchange.com/q/265/177">What are the differences between an elementCriteriaModel and fetched elements?</a></p>

<hr>

<p>If I understand your code correctly, you want to get all entries which have an empty <code>series</code>-field? </p>

<p>If so, you don't have to fetch all entries and check against it, you can just add this to the <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel" rel="nofollow noreferrer">ElementCriteriaModel</a>:</p>

<pre class="lang-twig prettyprint-override"><code>{% set MyCriteria = craft.entries.section('databytes').series(':empty:') %}

{% paginate MyCriteria as entries %}
....
{% endpaginate %}
</code></pre>

<hr>

<p><strong>Why does the pagination tag need a CriteriaModel?</strong></p>

<p>An ElementCriteriaModel can be compared to a sql query, it specifies what elements/entries to fetch (it sets the criteria). If you <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel#outputting-elements" rel="nofollow noreferrer">treat your CriteriaModel as an array or append .first()</a> it will fetch those entries and replace the results with the CriteriaModel.</p>

<p>The <a href="http://buildwithcraft.com/docs/templating/tags#paginate" rel="nofollow noreferrer">pagination-tag</a> needs a CriteriaModel (or "query") because it adds an offset (to the "query") depending on what page you are on, and then fetches the entries. - That's why the pagination-tag doesn't work with already fetched entries.</p>

CC BY-SA 3.0
2014-07-07T13:40:25.627Z
Hi Victor, thank you, that looks like just the solution I am after. I spent ages trying to figure out what I came up with to. Sorry but how Craft works with ElementCriteriaModels is till very confusing to me and I have read the docs and Stuarts post a few times. Could you expand on your example as the only pagination example I have is http://buildwithcraft.com/docs/templating/tags#paginate and I can't figure what to add to your solution to make it work. Many thanks for your help.
Gareth
CC BY-SA 3.0
2014-07-07T15:52:51.693Z
I added an explanation for the CriteriaModel. Copying the example pagination-code from the docs inside my example should work fine, what problems do you have exactly?
Victor
CC BY-SA 3.0
2014-07-07T16:14:23.557Z
Hi Victor, thank you for the explanation and I have updated my example to show you what I have which currently displays no results.
Gareth
CC BY-SA 3.0
2014-07-07T16:26:52.927Z
I tested your code and it worked in my test. What field type is `series` and what does it contain?
Victor
CC BY-SA 3.0
2014-07-07T16:45:24.360Z
`series` is an entries field type: http://buildwithcraft.com/docs/entries-fields and can contain multiple related entries.
Gareth
CC BY-SA 3.0
2014-07-07T16:55:27.923Z
Can you remove `.series(':empty:')` to check if all entries are fetched. If so, can you make sure that there is an entry where the `series` field is empty.
Victor
CC BY-SA 3.0
2014-07-07T17:07:06.853Z
Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/15587/discussion-between-gareth-redfern-and-victor-in).
Gareth
CC BY-SA 3.0
2014-07-07T13:11:44.463Z
2014-07-07T15:49:52.783Z
2017-04-13T12:50:59.930Z
Victor
<p>I have read through the <a href="http://buildwithcraft.com/docs/templating/tags#paginate" rel="nofollow">docs</a> and I am not sure if what I need to do is possible with array sets. I have filtered my entries using the following code"</p>

<pre class="lang-twig prettyprint-override"><code>{% set allDatabytes = craft.entries.section('databytes').limit(null) %}
{% set seriesCollection = [] %}

{% for databyte in allDatabytes %}

  {# get only the series databytes #}
  {% set series = databyte.series %}

  {# add the series databytes to the series collection #}
  {% for entry in series %}
    {% set seriesCollection = seriesCollection|merge([entry.title]) %}
  {% endfor %}

{% endfor %}

{# Output the filtered results #}

{% set filteredDatabytes = allDatabytes|without(seriesCollection) %}
&lt;ul&gt;
  {% for entry in filteredDatabytes %}
    &lt;li&gt;{{ entry.title }}&lt;/li&gt;
  {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>What I now need to do is add pagination to the <code>filteredDatabytes</code> set of content.</p>

<p><strong>From The Example Below</strong></p>

<p>This is my code which currently doesn't display any results. I guess you add the limit to the <code>MyCriteria</code> model (is that the correct term)?</p>

<pre class="lang-twig prettyprint-override"><code>{% set MyCriteria = craft.entries.section('databytes').series(':empty:').limit(10) %}

{% paginate MyCriteria as entries %}
    {% for entry in entries %}
        {{ entry.title }}
    {% endfor %}

    {% if paginate.prevUrl %}
        &lt;a href="{{ paginate.prevUrl }}"&gt;Previous Page&lt;/a&gt;
    {% endif %}

    {% if paginate.nextUrl %}
        &lt;a href="{{ paginate.nextUrl }}"&gt;Next Page&lt;/a&gt;
    {% endif %}
{% endpaginate %}
</code></pre>

<pagination>
Paginate An Array Of Entries
# Paginate An Array Of Entries

(Asked by: username_0 on 2014-07-07)

I have read through the docs and I am not sure if what I need to do is possible with array sets. I have filtered my entries using the following code"
{% set allDatabytes = craft.entries.section('databytes').limit(null) %}
{% set seriesCollection = [] %}

{% for databyte in allDatabytes %}

  {# get only the series databytes #}
  {% set series = databyte.series %}

  {# add the series databytes to the series collection #}
  {% for entry in series %}
    {% set seriesCollection = seriesCollection|merge([entry.title]) %}
  {% endfor %}

{% endfor %}

{# Output the filtered results #}

{% set filteredDatabytes = allDatabytes|without(seriesCollection) %}

  {% for entry in filteredDatabytes %}
    {{ entry.title }}
  {% endfor %}


What I now need to do is add pagination to the filteredDatabytes set of content.
From The Example Below
This is my code which currently doesn't display any results. I guess you add the limit to the MyCriteria model (is that the correct term)?
{% set MyCriteria = craft.entries.section('databytes').series(':empty:').limit(10) %}

{% paginate MyCriteria as entries %}
    {% for entry in entries %}
        {{ entry.title }}
    {% endfor %}

    {% if paginate.prevUrl %}
        Previous Page
    {% endif %}

    {% if paginate.nextUrl %}
        Next Page
    {% endif %}
{% endpaginate %}

**Tags:** pagination


---

## 2 answers

### Answer by username_1 on 2014-07-08. Score: 3 | Accepted answer

As username_2 rightfully points out, the {% paginate %} tags require an ElementCriteriaModel, so you can't do what you're doing in your first example because as soon as you call:
{% for databyte in allDatabytes %}

Craft hits the database and converts the ElementCriteriaModel into actual EntryModels.
We also don't have support, yet, for passing :empty: into things like AssetFields, TagFields, etc., which is why your second example won't work...  however that is on our list to add!
In this case, I think your best bet is going to be to do through the search service with something like this:
{# Search for 10 entries in the 'databytes' section where the 'series' assetField doesn't have anything selected. #}
{% set myCriteria = craft.entries.section('databytes').search('-series:*').limit(10) %}

{% paginate myCriteria as entries %}
    {% for entry in entries %}
        {{ entry.title }}
    {% endfor %}

    {% if paginate.prevUrl %}
        Previous Page
    {% endif %}

    {% if paginate.nextUrl %}
        Next Page
    {% endif %}
{% endpaginate %}

> **Comments:**
> **username_0**: Hi Brad, thank you that worked great. Its good to see there is so many different ways to attack a problem.  

---

### Answer by username_2 on 2014-07-07. Score: 1

This won't work with your example, because the pagination-tag only accepts an ElementCriteriaModel, not an array of already fetched entries:
{% paginate MyCriteria as PageSubset %}

See also: What are the differences between an elementCriteriaModel and fetched elements?

If I understand your code correctly, you want to get all entries which have an empty series-field? 
If so, you don't have to fetch all entries and check against it, you can just add this to the ElementCriteriaModel:
{% set MyCriteria = craft.entries.section('databytes').series(':empty:') %}

{% paginate MyCriteria as entries %}
....
{% endpaginate %}


Why does the pagination tag need a CriteriaModel?
An ElementCriteriaModel can be compared to a sql query, it specifies what elements/entries to fetch (it sets the criteria). If you treat your CriteriaModel as an array or append .first() it will fetch those entries and replace the results with the CriteriaModel.
The pagination-tag needs a CriteriaModel (or "query") because it adds an offset (to the "query") depending on what page you are on, and then fetches the entries. - That's why the pagination-tag doesn't work with already fetched entries.

> **Comments:**
> **username_0**: Hi username_2, thank you, that looks like just the solution I am after. I spent ages trying to figure out what I came up with to. Sorry but how Craft works with ElementCriteriaModels is till very confusing to me and I have read the docs and Stuarts post a few times. Could you expand on your example as the only pagination example I have is http://buildwithcraft.com/docs/templating/tags#paginate and I can't figure what to add to your solution to make it work. Many thanks for your help.  
> **username_2**: I added an explanation for the CriteriaModel. Copying the example pagination-code from the docs inside my example should work fine, what problems do you have exactly?  
> **username_0**: Hi username_2, thank you for the explanation and I have updated my example to show you what I have which currently displays no results.  
> **username_2**: I tested your code and it worked in my test. What field type is `series` and what does it contain?  
> **username_0**: `series` is an entries field type: http://buildwithcraft.com/docs/entries-fields and can contain multiple related entries.  
> **username_2**: Can you remove `.series(':empty:')` to check if all entries are fetched. If so, can you make sure that there is an entry where the `series` field is empty.  
> **username_0**: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/15587/discussion-between-gareth-redfern-and-victor-in).  

---

