<p>This is in no means an elegant solution, and can almost certainly be improved. But it did its job just fine.</p>

<pre><code>{% set veterinarians = craft.entries({
    section: 'veterinarians',
    order: 'premiumVet desc, postDate desc',
}) %}

{# more filter/search params #}

{% set entriesPerPage = 20 %}
{% set page = craft.request.getParam('page') %}
{% set page = page ?: 1 %}
{% set offsetParam = (page - 1) * entriesPerPage %}

{% set veterinarians = veterinarians.offset(offsetParam).limit(entriesPerPage) %}

{% set isNextPage = veterinarians.offset(page * entriesPerPage).limit(1)|length %}
{% set queryString = craft.request.getQueryStringWithoutPath()|replace('/page=\\d+&amp;?/') %}
{% set queryString = queryString ? '&amp;' ~ queryString %}
{% set nextUrl = isNextPage ? craft.request.url ~ '?page=' ~ (page + 1) ~ queryString %}

{% for entry in veterinarians %}
    {# loop entries #}
{% endfor %}

{% if nextUrl %}
    &lt;div class="js-pagination-trigger"&gt;
        &lt;a href="{{ nextUrl }}"&gt;Weitere anzeigen&lt;/a&gt;
    &lt;/div&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-11-19T22:02:42.967Z
Just a follow up to this now I look at it in more detail - it's a tad harder when combining two element types because you can't just use the offset + limit approach as easily on the queries -> because you need to do the bracketing on the sorted array.  But other than that it seems like it will be much the same.
Jeremy Daalder
CC BY-SA 3.0
2015-11-19T22:31:25.777Z
Yes right, I totally didn't notice that my code doesn't work for arrays of element models. I hope you find a working solution using Twigs slice filter (bracketing), but you could get into troubles with performance using such an approach rather soon. Not sure if there is a way to work with element IDs first, but it's probably tough to get this sorted with your different types. A custom plugin solution would be fastest, as you could query for a subset of your (mixed) elements for each pagination page.
carlcs
CC BY-SA 3.0
2015-11-19T23:46:13.523Z
Yes, the plugin seems the best way as if someone does a really broad search, hundreds or more entries could come back with the full query.  But honestly in our case I think very few would look beyond top 50 search results, so I am simply going to use some limits for now and get some feedback before solving a problem that might not even exist I think.  The purist in my wants to solve it, but the 'get this thing done' part of me is currently winning.....for now ;)
Jeremy Daalder
CC BY-SA 4.0
2018-10-10T07:34:55.017Z
Hi carlcs, this is a great solution. Can you update your answer for Craft 3 please?
Crafty Cat
CC BY-SA 3.0
2015-11-19T08:36:08.733Z
2015-11-19T08:36:08.733Z
carlcs
<p>I have to...</p>

<ul>
<li>merge results from multiple different criteria models</li>
<li>use Supersort to order my entries in the desired sort order</li>
<li>[...]</li>
</ul>

<p>...and need to paginate them for my index page. As the <a href="http://buildwithcraft.com/docs/templating/paginate" rel="nofollow"><code>paginate</code></a> tag doesn't work with arrays of element models, is there any other solution to get this working?</p>

<pagination><entrymodel><plugin-supersort>
Paginate entries when I do not have a criteria model?
# Paginate entries when I do not have a criteria model?

(Asked by: username_0 on 2015-11-19)

I have to...

* merge results from multiple different criteria models
* use Supersort to order my entries in the desired sort order
* [...]

...and need to paginate them for my index page. As the [`paginate`](http://buildwithcraft.com/docs/templating/paginate) tag doesn't work with arrays of element models, is there any other solution to get this working?

**Tags:** pagination, entrymodel, plugin-supersort

---

## 1 answers

### Answer by username_0 on 2015-11-19. Score: 4 | Accepted answer

This is in no means an elegant solution, and can almost certainly be improved. But it did its job just fine.
{% set veterinarians = craft.entries({
    section: 'veterinarians',
    order: 'premiumVet desc, postDate desc',
}) %}

{# more filter/search params #}

{% set entriesPerPage = 20 %}
{% set page = craft.request.getParam('page') %}
{% set page = page ?: 1 %}
{% set offsetParam = (page - 1) * entriesPerPage %}

{% set veterinarians = veterinarians.offset(offsetParam).limit(entriesPerPage) %}

{% set isNextPage = veterinarians.offset(page * entriesPerPage).limit(1)|length %}
{% set queryString = craft.request.getQueryStringWithoutPath()|replace('/page=\\d+&?/') %}
{% set queryString = queryString ? '&' ~ queryString %}
{% set nextUrl = isNextPage ? craft.request.url ~ '?page=' ~ (page + 1) ~ queryString %}

{% for entry in veterinarians %}
    {# loop entries #}
{% endfor %}

{% if nextUrl %}
    
        Weitere anzeigen
    
{% endif %}

> **Comments:**
> **username_1**: Just a follow up to this now I look at it in more detail - it's a tad harder when combining two element types because you can't just use the offset + limit approach as easily on the queries -> because you need to do the bracketing on the sorted array.  But other than that it seems like it will be much the same.  
> **username_0**: Yes right, I totally didn't notice that my code doesn't work for arrays of element models. I hope you find a working solution using Twigs slice filter (bracketing), but you could get into troubles with performance using such an approach rather soon. Not sure if there is a way to work with element IDs first, but it's probably tough to get this sorted with your different types. A custom plugin solution would be fastest, as you could query for a subset of your (mixed) elements for each pagination page.  
> **username_1**: Yes, the plugin seems the best way as if someone does a really broad search, hundreds or more entries could come back with the full query.  But honestly in our case I think very few would look beyond top 50 search results, so I am simply going to use some limits for now and get some feedback before solving a problem that might not even exist I think.  The purist in my wants to solve it, but the 'get this thing done' part of me is currently winning.....for now ;)  
> **username_2**: Hi carlcs, this is a great solution. Can you update your answer for Craft 3 please?  

---

