Skip to content

Instantly share code, notes, and snippets.

@mike-moreau
Created July 7, 2026 19:07
Show Gist options
  • Select an option

  • Save mike-moreau/d17ee709f0260b55638eaafb3fd703fc to your computer and use it in GitHub Desktop.

Select an option

Save mike-moreau/d17ee709f0260b55638eaafb3fd703fc to your computer and use it in GitHub Desktop.
Exclude Children of Draft Parents in Hugo Sitemap
{{/* gotmplfmt-ignore-all */ -}}
{{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
{{ range where .Pages "Sitemap.Disable" "ne" true }}
{{- $hasAncestorDraft := false -}}
{{- range .Ancestors -}}
{{- if .Draft -}}
{{- $hasAncestorDraft = true -}}
{{- end -}}
{{- end -}}
{{- if and .Permalink (not $hasAncestorDraft) -}}
<url>
<loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
<lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
<changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
<priority>{{ .Sitemap.Priority }}</priority>{{ end }}{{ if .IsTranslated }}{{ range .AllTranslations }}
<xhtml:link rel="alternate" hreflang="{{ .Language.Locale }}" href="{{ .Permalink }}"/>{{ end }}{{ end }}
</url>
{{ end -}}
{{- end -}}
</urlset>
@mike-moreau

Copy link
Copy Markdown
Author

Drafts are excluded from Hugo's sitemap automatically, but if you want to exclude pages nested under a draft parent you need to add a custom sitemap.xml file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment