2

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 tag doesn't work with arrays of element models, is there any other solution to get this working?

4

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 %}
    <div class="js-pagination-trigger">
        <a href="{{ nextUrl }}">Weitere anzeigen</a>
    </div>
{% endif %}
|improve this answer|||||
  • 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. – Jeremy Daalder Nov 19 '15 at 22:02
  • 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 Nov 19 '15 at 22:31
  • 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 Nov 19 '15 at 23:46
  • Hi carlcs, this is a great solution. Can you update your answer for Craft 3 please? – Crafty Cat Oct 10 '18 at 7:34

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.