👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Gatsby - How to Filter a List of Documents by Whether they are Published or Not

3 replies
Last updated: Feb 2, 2022
Hey, does anyone know how to filter a list of documents from Sanity by whether they are published or not? Currently I get all of them whether they’re draft or not.
Feb 2, 2022, 6:02 PM
in the plugin options for
gatsby-source-sanity
in
gatsby-config.js
you can set
overlayDrafts
to true/false to include or exclude draft documents. Typically one might set it to an env variable so you get drafts while testing, but not in production or whatever your preference:
{
      resolve: "gatsby-source-sanity",
      options: {
        projectId: "abc123",
        dataset: "production",
        watchMode: isDev,
        overlayDrafts: isDev,
        token: process.env.MY_SANITY_TOKEN,
      },
},

Feb 2, 2022, 6:23 PM
There is an example GROQ query here on how to not return items marked as draft, https://www.sanity.io/docs/drafts#ebfb408fa9b6
Feb 2, 2022, 6:35 PM
user N
Thank you, that’s perfect. Completely forgot about that option!
Feb 2, 2022, 8:12 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?