Random post in Hugo

Published on 2018-11-28 by Mohd

I wanted a way to link to a random post of the same content type. The solution wasn't readily available, I googled around and came up with this:

  {{ range first 1 (shuffle (where (where (where .Site.Pages "Type" "post") "Title" "!=" "posts") "Title" "!=" .Title)) }}
    <div>
      <a href="{{ .URL }}">Go to another random post</a>
    </div>
  {{ end }}

Kinda verbose isn't it? The 3 where clauses are to:

But it works, ish. The downside is, since Hugo is a static site generator, the random links are only generated once.