# Blog Listing Page Sort Functionality Not Working Properly

**URL:** <https://community.hubspot.com/t/blog-listing-page-sort-functionality-not-working-properly/108176>\
**Category:** CMS Development\
**Tags:** professional, enterprise\
**Created:** [May 29, 2024, 4:53am UTC](https://community.hubspot.com/t/blog-listing-page-sort-functionality-not-working-properly/108176 "2024-05-29T04:53:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![omev](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/omev/32/201427_2.png) [@omev](https://community.hubspot.com/u/omev)\
**Post date:** [May 29, 2024, 4:53am UTC](https://community.hubspot.com/t/blog-listing-page-sort-functionality-not-working-properly/108176/1 "2024-05-29T04:53:28Z")

</div>

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:

```lia-code-sample
{% 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.**

---

<div class="post-metadata">

**Author:** ![GRajput](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/grajput/32/102020_2.png) [@GRajput](https://community.hubspot.com/u/GRajput)\
**Post date:** [May 29, 2024, 7:35am UTC](https://community.hubspot.com/t/blog-listing-page-sort-functionality-not-working-properly/108176/2 "2024-05-29T07:35:40Z")

</div>

Hi @omev

You can use the “blog\_recent\_posts” function to list blogs by their created date. Here is the [doc](https://developers.hubspot.com/docs/cms/hubl/functions#blog-recent-posts) if you wanna know more.

```lia-code-sample
{{ 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!”

---

<div class="post-metadata">

**Author:** ![omev](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/omev/32/201427_2.png) [@omev](https://community.hubspot.com/u/omev)\
**Post date:** [May 29, 2024, 5:05pm UTC](https://community.hubspot.com/t/blog-listing-page-sort-functionality-not-working-properly/108176/3 "2024-05-29T17:05:48Z")

</div>

Thanks for your response [@Gau](https://community.hubspot.com/t5/user/viewprofilepage/user-id/506314)

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.

---

<div class="post-metadata">

**Author:** ![albertsg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/albertsg/32/99473_2.png) [@albertsg](https://community.hubspot.com/u/albertsg)\
**Post date:** [June 2, 2024, 7:04pm UTC](https://community.hubspot.com/t/blog-listing-page-sort-functionality-not-working-properly/108176/4 "2024-06-02T19:04:30Z")

</div>

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:

```lia-code-sample
{% 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: [Blog templates - HubSpot docs](https://developers.hubspot.com/docs/cms/building-blocks/templates/blog)

---

<div class="post-metadata">

**Author:** ![omev](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/omev/32/201427_2.png) [@omev](https://community.hubspot.com/u/omev)\
**Post date:** [June 4, 2024, 5:57pm UTC](https://community.hubspot.com/t/blog-listing-page-sort-functionality-not-working-properly/108176/5 "2024-06-04T17:57:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![albertsg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/albertsg/32/99473_2.png) [@albertsg](https://community.hubspot.com/u/albertsg)\
**Post date:** [June 4, 2024, 8:19pm UTC](https://community.hubspot.com/t/blog-listing-page-sort-functionality-not-working-properly/108176/6 "2024-06-04T20:19:36Z")

</div>

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](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 🙂
