Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
How to sort blog posts by a custom field
Participant
Sep 9, 2019 4:26 PM - edited Sep 10, 2019 10:04 AM
We ran into this issue where we wanted to sort items from a blog based on the contents of a custom field/module we added to that post type.
We noticed there are no solutions for this yet and discovered one that utilizes some undocumented features of Hubl. We're sharing it here for anyone else that might find it useful:
{# Detail Template -- Sorting Field #} {% text 'sorting' label='Sorting Order #', no_wrapper=True, overrideable=True, export_to_template_context=True %} {# Listing Template -- Sorting Logic #} {% set post_list = blog_recent_posts('xxx', 999) %} {% set post_list_sortable = [] %} {% for post in post_list %} {# Assign the index as a fallback, un-ordered items appear below explicitly ordered ones #} {% set sort = post.widgets.sorting.body.value + 'x' + loop.index %} {% set tmp = post_list_sortable.append({ 'sorting': sort, 'record': post}) %} {% endfor %} {# Templating Output #} {% for post in post_list_sortable|sort(false, false, 'sorting') %} {% set post = post.record %} <h2>{{post.title}}</h2> {% endfor %}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content