CMS Development

omev
Member

Blog Listing Page Sort Functionality Not Working Properly

SOLVE

Hi there,

 

I have built a Blog Listing Page which is expected to show 6 blog  tiles at a time on each page, sorted by the date they were each created. The issue I am having is that they are not coming back in the exact correct order. The order is close to being correct, but it is slightly mixed up. 

 

I have followed these steps:

1. I opened "Blog Settings", clicked on the "Templates" tab, and under "Number of posts per listing page" I set the number to 6. This is working as expected

2. In my code, I added the following to sort the blogs:

 

{% for content in contents|sort(False, False, 'created') %}
 {# The HTML for the tiles gets populated in the javascript #}
  <a
    href={{ content.absolute_url }}  
    class="tile" 
    data-blog-tile 
    data-createddate="{{ format_datetime(content.created, dateFormat) }}"
    data-featuredimage="{{ content.featured_image }}"
    data-taglist="{{ content.tag_list }}"
    data-title="{{ content.name }}"
    data-href="{{ content.absolute_url }}"
  />
{% endfor %}

 

 

I can validate that the blog posts are not coming up in the exact order by going to the Blog Posts page, and sorting them by Created Date. I can see that the first 5 I get in the response on the Listings Page are correct, but then the 6th one is out of order. On the second page, where I expect to get the next 6, the first 3 are in the correct order, and then the last 3 are mixed up. 

 

Has anyone ever experienced this before? Any helps is greatly appreciated!

*Update: I am noticing that the 6 blog posts that are showing up are actually the 6 most recently published, and the code I have added to sort the blog posts is sorting those 6 blog posts by the created date.  What I am trying to achieve is to get the 6 most recently created as part of the original response. Does anyone know if this is possible? @albertsg this is in line with what you mentioned about the original response being sorted by the publish date.

1 Accepted solution
albertsg
Solution
Guide

Blog Listing Page Sort Functionality Not Working Properly

SOLVE

Hi @omev

 

Let me tell you more about my experience with blog listing.

 

In one of the projects I was working on, I was requested to create a "different" listing for the blog, not by created date but by other factors. 
For the initial X articles it was not a problem, but when the number of articles started to grow, I was facing quite some critical challenges: 

 

- HubSpot returns the articles inside the {{contents}} variable, which is sorted by published date. You can sort that list out (sorting can be challenging as you are experiencing - I've had some long conversations with HubSpot support in the past regarding this topic) but things will start to get complicated when you have more than X articles and you need to start doing pagination. You will have to create your own custom pagination behaviour, which again can be tricky as you have to not just order {{contents}} but also make sure you are seeing the correct articles for the pagination page you are in. 

- All this sorting comes with a performance impact that might affect the user experience on your site, conversion rates, etc. 

- I wrote a bit about this issue here, maybe it helps you see other possible issues you might encounter: https://community.hubspot.com/t5/HubSpot-Ideas/Blog-listing-why-there-is-a-limit/idi-p/410514 


Because of this, I encourage you to stick with HubSpot's "default" functionality. You can always Unpublish and Publish again articles in a past date and sort them in the order you want, but of course that's just my suggestion 🙂



Did my answer help you? Mark it as a solution

View solution in original post

0 Upvotes
5 Replies 5
albertsg
Guide

Blog Listing Page Sort Functionality Not Working Properly

SOLVE

Hi @omev

I believe blogs are ordered by Published Date and not by Created Date. 

 

You shouldn't need to use any filter for sorting, {{contents}} should include the blogs already ordered. Important to note that you can modify that order by Unpublishing and Publishing again blog posts (you can publish them in a past date as well). 

The following code should be enough:

 

{% for content in contents %}
    <div class="post-item">
        Post item markup that renders with each iteration.              
    </div>
{% endfor %}

 

But if you want to list your blogpost using a different date, then you will need a |sort filter, yes (altough I personally don't recommend this approach as it can get messy once the Blog content grows). 

 

I recommend you to take a look at this page: https://developers.hubspot.com/docs/cms/building-blocks/templates/blog 



Did my answer help you? Mark it as a solution
0 Upvotes
omev
Member

Blog Listing Page Sort Functionality Not Working Properly

SOLVE

Hi @albertsg , thank you for your response!

 

I am actually looking to sort the blogs by their Created Date, and not by the Publish Date. That is why I have implemented the sort filter.

 

I am curious what you mean by this comment:

"(altough I personally don't recommend this approach as it can get messy once the Blog content grows)." - What kind of "messiness" have you experienced in the past? Is having blog posts return in the incorrect order something that you have seen previously?

0 Upvotes
albertsg
Solution
Guide

Blog Listing Page Sort Functionality Not Working Properly

SOLVE

Hi @omev

 

Let me tell you more about my experience with blog listing.

 

In one of the projects I was working on, I was requested to create a "different" listing for the blog, not by created date but by other factors. 
For the initial X articles it was not a problem, but when the number of articles started to grow, I was facing quite some critical challenges: 

 

- HubSpot returns the articles inside the {{contents}} variable, which is sorted by published date. You can sort that list out (sorting can be challenging as you are experiencing - I've had some long conversations with HubSpot support in the past regarding this topic) but things will start to get complicated when you have more than X articles and you need to start doing pagination. You will have to create your own custom pagination behaviour, which again can be tricky as you have to not just order {{contents}} but also make sure you are seeing the correct articles for the pagination page you are in. 

- All this sorting comes with a performance impact that might affect the user experience on your site, conversion rates, etc. 

- I wrote a bit about this issue here, maybe it helps you see other possible issues you might encounter: https://community.hubspot.com/t5/HubSpot-Ideas/Blog-listing-why-there-is-a-limit/idi-p/410514 


Because of this, I encourage you to stick with HubSpot's "default" functionality. You can always Unpublish and Publish again articles in a past date and sort them in the order you want, but of course that's just my suggestion 🙂



Did my answer help you? Mark it as a solution
0 Upvotes
GRajput
Recognized Expert | Gold Partner
Recognized Expert | Gold Partner

Blog Listing Page Sort Functionality Not Working Properly

SOLVE

Hi @omev 

You can use the "blog_recent_posts" function to list blogs by their created date. Here is the doc if you wanna know more.

 

{{ blog_recent_posts("default", 5) }}
{% set rec_posts = blog_recent_posts("default", 5) %}
{% for rec_post in rec_posts %}
    <div class="post-title">{{ rec_post.name }}</div>
{% endfor %}

 

"I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member.
Thanks!"

 




Gaurav Rajput
Director, MarTech( Growth Natives)

Book a meeting


0 Upvotes
omev
Member

Blog Listing Page Sort Functionality Not Working Properly

SOLVE

Thanks for your response @Gau 

 

Unfortunately this workaround did not solve the problem. The blog posts were coming back in the exact same order as they did with the code that I had used. 

0 Upvotes