Ordering of two Blog Posts on the Same Day with Hugo

If you are using Hugo you will know about the date field in the front matter and how it orders the posts you publish. Normally I use year, month, day (yyyy-MM-dd). But there have been a few occasions where I wanted to publish two blog posts on the same day, and also wanted to specify which would appear higher on the page.

tl;dr: add hours, minutes, seconds to the date, e.g. date: "2022-01-07T00:00:01Z" and date: "2022-01-07T00:00:00Z", the newer one will be higher up the list.

The problem

By default, if two posts have the same date, the title of the post determines the order.

But it may not be convenient to change the titles to get the ordering you want.

If I had two posts, and I want this one to appear on top -

---
title: Top
date: "2022-01-07"
---

Should be the Top post for today

And this, to appear below the top one -

---
title: Must be below Top
date: "2022-01-07"
---

Should be below the Top post

But they don’t appear in the desired order -

The fix

Ordering by date is a higher priority than ordering by title, all I need to do is give one of the posts an earlier date and one a later date, the later date will appear on top.

Fortunately, there are a lot of hours, minutes, and seconds in the day, and adding these to the date field gives me all the flexibility I need. I set the date of one post to midnight and the other to one second after midnight.

This one will appear higher on the list because it is the newer of the two posts.

---
title: Top
date: "2022-01-07T00:00:01Z"
---

Should be the Top post for today

This one will appear lower because it is older.

---
title: Must be below Top
date: "2022-01-07T00:00:00Z"
---

Should be below the Top post

Now, they are in the correct order -

comments powered by Disqus

Related