I know about buildFuture
, but I’m wondering if there is a way to use Hugo’s built-in date sorting and formatting to make sure Hugo doesn’t publish outdated content. For example, let’s say the publishDate for a post is 2015-01-19, and I want to make sure that posts older than a year old are not part of my Hugo build, is there a way to do this (something akin to an “older than” filter). Then I could at least create a separate list page for managing the content to manually remove content after a 1-year expiration. This would also come in handy for a content type like “events,” where I might want to only publish upcoming events and event-related materials for the last year. Any thoughts on how to get this done @moorereason or @digitalcraftsman ?
After fiddling a while with the template functions I compiled this small code snippet:
{{ range where .Data.Pages ".Date.Unix" "ge" (sub .Now.Unix 31536000)}}
{{ end }}
The script above compares two UNIX time stamps. Here is a small summary if you’re not familar with UNIX time stamps: this value is just a counter of all seconds since January 1st 1970. That makes it very easy to store and compare time stamps (e.g. without the hassle of timezones, leap years etc.).
31536000 is the number of seconds of one year (360024365). I’ve subtracted one year (in seconds) from the current date and looked if a post’s creation date is greater. This would mean that the post isn’t older than a year.
Hopefully, I explained this understandably. However, if your posts have a special content type like “events” you have to filter them again before you can range over them.
{{ $posts := where .Site.Recent ".Type" "events" }}
@digitalcraftsman Again, you’ve come through. Thanks so much, brother! I will have to test this when I get home, but I’m not currently leveraging date
as much as I am publishDate
and updateDate
in article-level YAML for a POC I’m working on at my job. I’m assuming I can substitute .Date.Unix
with .PublishDate.Unix
instead? I owe ya.
Also, I feel like an ass since I now realize that this question is covered in the docs more or less at the bottom of this page. Cheers!
Replacing .Date.Unix
with .PublishDate.Unix
should work fine. If you encounter issues please report them back.
It’s sometimes possible that the docs contain the foundation of your problem in some hidden corner. I haven’t memorized the docs too.