Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Discussion about filtering out draft articles in Sanity.io API query

18 replies
Last updated: Mar 8, 2022
Hi team, why this is not filter out draft articles https://*******.api.sanity.io/v1/data/query/pre-prod?query=*[_type=='article' && !(_id in path('drafts.*'))]
Mar 7, 2022, 9:32 PM
It's currently looking for something that exactly matches
drafts.
Does it work if you change it to
!(_id in path('drafts.**'))
? Also, it looks like you're using
v1
of the API. You may also want to change over to a more recent version .
Mar 7, 2022, 9:35 PM
without url encoding
Mar 7, 2022, 9:42 PM
once url encoded, I will get a result but draft articles also included in the result
Mar 7, 2022, 9:44 PM
drafts not filter out from above URL as well, updated api version and used this filter !(_id in path('drafts.**'))
Mar 7, 2022, 9:54 PM
Where are you running this query? If you run it in Vision do you get the correct results? I can't verify it since you've blocked out your document id.
Mar 7, 2022, 10:01 PM
Where are you running this query? If you run it in Vision do you get the correct results? I can't verify it since you've block out your document id.
Mar 7, 2022, 10:01 PM
Thank you very much Racheal, I'll test this on vison and let you know.
Mar 7, 2022, 10:03 PM
by default is it send two article objects if article is in draft status
Mar 7, 2022, 10:25 PM
What do you get if you run the query with the snippet I gave:
*[_type == 'article' && !(_id in path('drafts.**'))]
. Again, also make sure that you're not using v1 of the API in Vision.
Mar 7, 2022, 10:33 PM
I got it šŸ™‚. once you create a article and if you never published it, then it has only a draft id. when you filter out articles from this !(_id in path('drafts.**')) those never published articles will get filtered but articles which are published at first then edited by someone won't get filtered.
Mar 7, 2022, 10:46 PM
Thank you very much for your quick response and guidance šŸ˜Š
Mar 7, 2022, 10:49 PM
Happy to help! It's always fun to see folks have this start to click for them!
Mar 7, 2022, 10:49 PM
Hi
user M
is there a way of filter out both draft and previous state from the article list if article has a draft version.
Mar 8, 2022, 6:16 AM
I want to remove both 1 and 5 due to 'content test' article has a it's draft version. and I just want to select only 0, 2, 3, 4 from this article list.
Mar 8, 2022, 6:20 AM
Hey Dineth! You'd use the same method for excluding drafts that we worked through yesterday. As soon as edits are made to a published document, the
_id
will have the
drafts.
prepended to it again.
Mar 8, 2022, 5:50 PM
yes, got it. thank you Racheal šŸ™Œ
Mar 8, 2022, 5:53 PM
You're welcome!
Mar 8, 2022, 5:54 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?