Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

Separating documents in Studio by published and unpublished filters

4 replies
Last updated: May 25, 2022
Anyone know how to separate documents within the Studio by two filters:• published & drafts of published
• unpublished
I’m using code from docs, but the end result is:
• published & drafts of published
• unpublished & drafts of published
How do I specify that I want unpublished drafts only? I do not want drafts of published docs.


S.list()
    .title("Pages")
    .items([
      S.listItem()
        .title("Published")
        .child(
          S.documentList()
            .title("Published")
            .defaultOrdering([{ field: "title", direction: "asc" }])
            .filter(
              '_type == "page" && !(_id in path("drafts.**"))'
            )
            .child((id) =>
              S.document()
                .schemaType("page")
                .documentId(id)
                .views([S.view.form(), viewPreview])
            )
        ),
      S.listItem()
        .title("Unpublished")
        .child(
          S.documentList()
            .title("Unpublished")
            .defaultOrdering([{ field: "title", direction: "asc" }])
            .filter(
              '_type == "page" && (_id in path("drafts.**"))'
            )
            .child((id) =>
              S.document()
                .schemaType("page")
                .documentId(id)
                .views([S.view.form(), viewPreview])
            )
        ),
    ])
),
May 13, 2022, 4:34 AM
There's not an easy way to groq whether or not a document has been published, then reverted back to a draft because of edits. Usually, folks will set a
publishedOnce
hidden boolean that they update with a document action when they publish.
It's not the most elegant solution but we are working on improvements.
May 16, 2022, 3:43 PM
Ahh ok, thats a nice way to handle. Thanks
user M
🙏
May 16, 2022, 7:27 PM
You're welcome!
May 16, 2022, 7:57 PM
user M
user M
Sorry.. I'm late to the party, but I think I do what you need in one of my more "colorful" queries:


{
  'set': *[ _type == $type && _updatedAt > '2022-05-25' ]{_id}
}
{
  'drafts':  set[_id in path("drafts.**") ]{..., 'uid': _id},
  'published': set[!(_id in path("drafts.**"))]{..., 'uid': "drafts." + _id},
}
{
  'published-&-drafts-of-published': 
    published + drafts[ uid in ^.published[].uid ],
  'unpublished': 
    drafts[ !(uid in ^.published[].uid) ],
}
I can try to explain if anyone reads this, and wants me to
🤓
May 25, 2022, 8:19 AM

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?