Discussion about filtering out draft articles in Sanity.io API query
18 replies
Last updated: Mar 8, 2022
D
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
R
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
v1of the API. You may also want to change over to a more recent version .
Mar 7, 2022, 9:35 PM
D
without url encoding
Mar 7, 2022, 9:42 PM
D
once url encoded, I will get a result but draft articles also included in the result
Mar 7, 2022, 9:44 PM
D
Mar 7, 2022, 9:53 PM
D
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
R
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
R
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
D
Thank you very much Racheal, I'll test this on vison and let you know.
Mar 7, 2022, 10:03 PM
D
by default is it send two article objects if article is in draft status
Mar 7, 2022, 10:25 PM
R
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
D
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
D
Thank you very much for your quick response and guidance π
Mar 7, 2022, 10:49 PM
R
Happy to help! It's always fun to see folks have this start to click for them!
Mar 7, 2022, 10:49 PM
D
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
D
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
R
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
_idwill have the
drafts.prepended to it again.
Mar 8, 2022, 5:50 PM
D
yes, got it. thank you Racheal π
Mar 8, 2022, 5:53 PM
R
You're welcome!
Mar 8, 2022, 5:54 PM
Sanityβ build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.