Build draft or future (posts, maybe other content) but exclude them from published listings

Hi, I just came across a few threads (like List all posts but drafts or Keeping drafts out of the rss feed), I’m still fuzzy on how to actually make it happen.

I would like to build future and draft posts content, but not list them in the usual lists (tags, rss, posts), however I would like to list then in a specific path (e.g. host.tld/d/ ). So I’d like to hide draft posts anywhere exept when explicitly asked.

Currently I’m having difficulty to just create a list in a /d/ path, I have tried various combinations

  • content/d/_index.md or without
+++ 
draft = true 
title = "Drafts" 
+++

  • layouts/d/index.html or layouts/d/list.html
{{ define "header" }}
    {{ partial "header.html" . }}
{{ end }}

{{ define "content" }}
    {{ partial "drafts/content.html" . }}
{{ end }}

{{ define "footer" }}
    {{ partial "page-list/footer.html" . }}
{{ end }}

<!-- https://gohugo.io/templates/lists/ -->
  • layouts/partials/drafts/content.html
<span class="section__title">{{ .Title }}</span>
<ul class="posts drafts">
    {{ $paginator := .Paginate (where (where .Site.Pages "Type" "in" "posts") "Draft" true ) }}
    {{ partial "pagination.html" . }}
    <br/>
    {{ range $paginator.Pages }}
    <li>
        <span class="list__title--small">
            <time class="hidden-tablet">{{ .Date.Format (.Site.Params.dateformat | default "Jan 02 '06") }}</time>
            <a href="{{ .RelPermalink }}" {{if .Draft}}class="draft"{{end}}>{{ .Title }}</a>
        </span>
    </li>
    {{ end }}
</ul>

But of course the given setup is wrong, either the HTML is empty or the page only contains what’s _index.md but nothing else. Did I missed something in https://gohugo.io/templates/lists/.


Also I wonder, even if this works I’ll probably have to go through every template (RSS, other taxonomies) to filter drafts right ?

You need to use the recent Build Options and set the list parameter to false to hide content from list pages.

Regarding drafts and future posts you will need to set buildDrafts and buildFuture respectively to true in the project config or use -D, --buildDrafts and -F, --buildFuture in your deploy script. By default these pages are not published, hence the empty page that you have encountered.

1 Like

@alexandros Hi sorry for the silence, and many thanks for this tip, I just had the chance to try that, it works at the exception of taxinomies, i.e. tag or series lists draft content, but this may be due to the theme templates.

However your answer I still don’t quite understand how to make a /draft/ page that can list draft with this setting.

What do you mean?

If you mean creating a list page to display all drafts located at /draft/ then you need to create a template and render the drafts using where and Type in the range function.

In your first post you indicated that you wish to hide drafts and future posts from lists. If you want to display them in selected lists then you should not use the Build Options.

Instead you will have to go through all list templates and filter drafts and future posts.

1 Like

Hi yes to be more specific I hoped to render draft content and list draft in a single place only, without having to find all lists. E.g. like a special taxonomy for draft.

I gather from your answers this is not possible at this time to isolate draft in a single place (either they are listed anywhere or they aren’t).

I’ll see what works best for my use case.

Cheers.

For reference, here’s what I did, as I wasn’t satisfied by the build parameters in front matter:

---
# front-matter
_build:
  list: never
---

While it did prevent draft content from appearing in main lists of published content (rss, sitemap, taxonomies, etc.), I couldn’t list them anymore in a specific path to retrieve them easily.

So instead I chose a custom approach, I moved the draft content in a content/draft section, and moved draft posts in this section. Currently in order to avoid the modification of taxonomy pages like tags or series, these are commented in the front-matter of the posts pages (that may change in the future).

Draft section wasn’t displayed as expected, I needed to create a new default section layout, inspired by section doc.

I had to modify the following default Hugo templates as well.

And of course I needed to tweak the options of the hugo render in the Github Actions job.

Finally I wanted to tweak the appearance of the body for draft blog post. Given the theme I chose, I needed to add specific baseof layout for single page

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.