Add a Published Filter To Your Gatsby MDX Posts
Updated: October 3, 2020A published
filter is a great feature to filter your draft post, so that you can keep working on a it until it's ready to be shared with everyone.
We can implement this feature by adding a boolean value named published
in the frontmatter of your post.
If we set this boolean to true, the post will be visible to the world! Otherwise, the post is visible only to you.
Let's implement this feature in a Gatsby post.
Add published to the frontmatter
We can start by adding the published
boolean to the frontmatter of all our existing posts:
Adding the filter to the GraphQL queries
Once we have added the boolean value to all our post, we need to update the GraphQL queries in order to filter out any post where published
is set to false.
Inside your gatsby-node.js
file, update the queries that fetch your MDX posts and the one for your tags (if you have this feature implemented on your website):
Additionally, if you have an RSS Feed integrated into your website, you need to modify also the gatsby-config.js
file:
Once we have updated the GraphQL queries contained into the Gatsby configuration files, it's time to update the query that fetches for our posts inside the postTemplate
file:
Finally, for those of you who have integrated tags and categories into your website, remember to update also the GraphQL queries inside your tags.js
page and tagTemplate
component.