Sort & Paginate blog posts

Solved
jao
Excursionist
17 0 1

(asking again because I didn't get any replies on my previous 2 posts and Shopify support staff refuses to provide support)


I'm trying to create a paginated list with blog articles that are sorted by time of update.

But it results in this error:

Liquid error (sections/blog-template.liquid line 72): Array 'blog_posts_sorted' is not paginateable.

This is the relevant liquid code:

 

{% assign blog_posts_sorted = blog.articles | sort: "updated_at" %}

{%- paginate blog_posts_sorted by articles_per_page -%}

How do I paginate a sorted list of blog articles?

0 Likes
jao
Excursionist
17 0 1

BUMP

0 Likes
gina-gregory
Shopify Expert
710 48 180

You can't mess with the pagination/sort of blog posts with liquid. You can do some hacky things with javascript though.

jao
Excursionist
17 0 1
Thanks, that is good to know. Do you perhaps have any links to the docs that would guide me in the general direction?
0 Likes
gina-gregory
Shopify Expert
710 48 180

This is an accepted solution.

Generally speaking, you can gather all your posts into a JSON array by fetching the results of each paginated page, then sort that by the updated_at field. Then output the articles and add a javascript-based pagination system. If you have a ton of posts, this could get slow, but you can try to cache the results in localStorage or something so only the first load is slow.

jao
Excursionist
17 0 1

Awesome! Thanks, I’ll dig into that and see what I can find.

0 Likes