Blog: Abel G. Vidal

Hugo: filter post by tag

I wanted to stop showing in the frontpage of this blog powered by (Hugo) the posts tagged as “TIL” or “IWL”.

My theme template is

{{ $blogPages := where .Site.RegularPages.ByDate.Reverse ".Type" "in" .Site.Params.mainSections }}

I tried with “intersect” and “where” but it did not work, not sure if it is because my Hugo version is old but, anyway, I was too lazy to upgrade Hugo (and adapt all templates, breaking changes, etc) so this simple code made my day:

{{ $blogPages := where .Site.RegularPages.ByDate.Reverse ".Type" "in" .Site.Params.mainSections }}


{{ $filteredPages := slice }}
{{ range $blogPages }}
    {{ $hasTIL := false }}
    {{ $hasIWL := false }}
    
    {{ range .Params.tags }}
        {{ if eq . "TIL" }}{{ $hasTIL = true }}{{ end }}
        {{ if eq . "IWL" }}{{ $hasIWL = true }}{{ end }}
    {{ end }}
    
    {{ if and (not $hasTIL) (not $hasIWL) }}
        {{ $filteredPages = $filteredPages | append . }}
    {{ end }}
{{ end }}

{{ $blogPages = $filteredPages }}

It should be very easy to modify it to filter by. This version is exclude by.