Share Your Work

lformdesign
参加者

How to sort blog posts by a custom field

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 %}
5件の返信
mii
投稿者 | Platinum Partner
投稿者 | Platinum Partner

How to sort blog posts by a custom field



Quick Solution:
(Blog-Index)

<div class="flexbox">
 {% for content in contents %}
  {% set widget_value = content.widgets.blog_post_sorting.body.value %}
  {% set sorting_value = widget_value ? widget_value : 0 %}
  <div class="item" style="order:{{ sorting_value }};">(...)</div>
 {% endfor %}
</div>


!important Use flexbx for conteiner on listing-page and use the order stylsheet.
A Complete Guide to Flexbox | CSS-Tricks - CSS-Tricks

Custom Module: (Blog-Post)
sorting_blog_post_quickfix.PNG


Create a custom module, see Screenshot and place it to your blog-post template. 

{% module "blog_post_sorting" path="../../modules/blog_post_sorting", export_to_template_context=True %}


!important Set export_to_template_context=True. Then check your listing-page for content.widgets.blog_post_sorting.body.value.

Conclusion: This quick solution does not work with the blog pagination. Because it does not sort the HubSpot contents object. To work well, we have to avoid the pagination by increasing the amount of displayed blog articles in the blog settings. If your blog has a lot of articles, it is strongly recommended to use own filters or the existing tag filter and the default sorting by HubSpot.

MiaSrebrnjak
コミュニティーマネージャー
コミュニティーマネージャー

How to sort blog posts by a custom field

Thank you for sharing @mii👍 


Wusstest du, dass es auch eine DACH-Community gibt?
Nimm an regionalen Unterhaltungen teil, indem du deine Spracheinstellungen änderst


Did you know that the Community is available in other languages?
Join regional conversations by
changing your language settings

0 いいね!
crowell-tailord
参加者

How to sort blog posts by a custom field

Not sure if this is newer code functionality than your original post, but this is how we got around to doing it --

 

The widgets.module_XX.body.event_countdown.start_date_time is a custom field we use for defining an Event's start date. 

 

{% for content in contents|sort(false, false, 'widgets.module_1587729346876105.body.event_countdown.start_date_time') %}
0 いいね!
Anonymous
適用対象外

How to sort blog posts by a custom field

Great stuff - thanks for sharing!

0 いいね!
jennysowyrda
コミュニティーマネージャー
コミュニティーマネージャー

How to sort blog posts by a custom field

Thank you for sharing @lformdesign!

0 いいね!