Hide draft posts in Jigsaw static site generator

Swapnil Bhavsar • April 23, 2020

jigsaw tips

A few days ago I decided to build this blog using the Jigsaw blog starter template. It was super a fun process, I was able to build the blog in no time. However, I got stumbled upon draft posts. I was not able to figure out how to hide the draft post. After a few tries, I was able to find this solution in Jigsaw documentation.

The trick is to filter the posts collection in the production build. Jigsaw allows you to filter the item collection by adding a filter key to the post's YAML front matter.

For example, if you use published key in the YAML front matter of your blog post, you can filter the post-collection to get only published posts by adding the following code in config.php or config.production.php for a production build.

config.production.php

return [
    ...
    'collections' => [
        'posts' => [
            'filter' => function ($post) {
                return $post->published;
            }
        ],
    ],
];

Now, if you want to hide a draft/unpublished post from the production build, add a filter key published: false to the post's YAML front matter.

---
title: My draft blog post!
published: false
---

And vice versa if you want to show published blog posts like this.

---
title: My published blog post!
published: true
---

That's it! Enjoy blogging & creating sites with Jigsaw. If you have questions, let me know on Twitter @swapnil_bhavsar.