How to handle "expired" posts?

I have a set of products.

After a certain time period, I’d like these posts to “expire” so that they do not appear in the list page, but the single post still needs to exist. The functionality I’m looking for is similar to sites like eBay, where an expired auction does not appear in the main results, but the actual listing still exists.

I’ve been achieving this by trying two different things:

{{range .Pages}}
  {{if not .Params.expire}}
    ...
  {{end}}
{{end}}

.Params.expire is set to true for posts that have expired.

The other method I’ve tried is

{{range .Pages}}
  {{if lt now .Params.valid_through}}
   ...
  {{end}}
{{end}}

.Params.valid_through is a date set in front matter.

This logic is adding quite a bit to build time. I have about 2000 posts with half being expired. Is there a more efficient way of doing this?

Hugo’s where function can also make date-based comparisons. That makes it possible to range only over a small subset of pages.

You likely also want to use now to access the current date. And with AddDate you can construct new future (and past!) dates.

As an example, this is code I use in my website to get all content that’s published in the last 1.5 month:

where .Site.RegularPages "Lastmod" "gt" (now.AddDate 0 -1 -15)
1 Like

Good suggestion. Thanks.

Not sure if I can make your construct work as there is not a predefined date that my posts will expire. So might expire in 7 days, others in 14 days etc. That’s why I created a custom variable in front matter.

Also see this comment:

I have it implemented, but I got distracted … but not long.

1 Like