Querying archived posts in Sanity.io causing unexpected results
13 replies
Last updated: May 9, 2023
J
So I am running into what I think is an odd issue where my query looks like this
As when I inspect my post I see
or even
the posts are hidden that are listed as archived
export const allPosts = groq`*[_type == "post" && isArchived != true]{ ${postFields} }`
isArchived:trueand I don't want to display posts that are listed as archived. However, this still shows that post. If I update the query to
export const allPosts = groq`*[_type == "post" && isArchived == false]{ ${postFields} }`
export const allPosts = groq`*[_type == "post" && !isArchived]{ ${postFields} }`
May 9, 2023, 2:45 PM
A
Which API version?
May 9, 2023, 2:46 PM
A
Also,
isArchived != truewill also match any post that does not have the
isArchivedattribute, or where it's null.
May 9, 2023, 2:47 PM
J
I have
And that is fine if it matches posts that don't have
export const apiVersion = process.env.NEXT_PUBLIC_SANITY_API_VERSION || '2023-01-01'
isArchivedor it's null. However, my issue is that it was still matching posts that had it set to
true.
May 9, 2023, 2:49 PM
A
I don't know what API version that is, just make sure you are using
v2022*or
v2023*.
May 9, 2023, 2:50 PM
A
That is odd, if so. This is an extremely well-tested type of query. Can you reproduce it with a single query URL or using the Vision plugin?
May 9, 2023, 2:51 PM
J
Oh I have always see a full date used for
But examples like
this show the
apiVersionπ€ even in
user G
tutorial she says you can put any date you want basically. The vision plugin works as expected but the API version shows v2021-10-21π€
But examples like
this show the
apiVersionas
2022-11-15π€
May 9, 2023, 2:55 PM
A
Yes, you need a full date. I mean you need an API version that starts with 2022 or 2023.
May 9, 2023, 2:56 PM
J
π€¦ββοΈ the problem was me π
So I have
where the initial value
should be false but somehow my editor got a state where the boolean is right in the middle and that was the one that kept showing up when
defineField({ type: 'boolean', name: 'isArchived', title: 'Archive Post?', description: 'Archived posts will not be shown in the front-end', initialValue: false, }),
should be false but somehow my editor got a state where the boolean is right in the middle and that was the one that kept showing up when
isArchived !== truebut disappearing when I did
!isArchivedπ
May 9, 2023, 3:06 PM
J
What would be the safer bet? Just to do
!isArchived?
May 9, 2023, 3:06 PM
A
I think that's clean
May 9, 2023, 3:15 PM
J
Same, I just don't know how the post got into a null state as when I created a new post it was in the false state and even toggling it, it still did not get into the null state π€
May 9, 2023, 3:16 PM
A
If you want to handle the null state too, there's nothing wrong with
!= trueeither. π
May 9, 2023, 3:16 PM
J
Yea that might be safer, my client is a senior citizen building a website for senior citizens in our city so whatever I can do to make it as bullet proof as possible I want to do.
May 9, 2023, 3:18 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.