👀 See Sanity in action: Watch product demo now →

Hey all, let’s say I have content types ‘StandardPost’ and ‘ReviewPost’. They are identical except review post has the extra fields ‘rating’, ‘pros’ and ‘cons’. I want my...

6 replies
Last updated: Nov 1, 2020
Hey all, let’s say I have content types ‘StandardPost’ and ‘ReviewPost’. They are identical except review post has the extra fields ‘rating’, ‘pros’ and ‘cons’. I want my Category pages to list and sort all posts, regardless of whether they are StandardPost or ReviewPost. Would the following GROQ query be valid and accomplish this?
*[_type == “standardPost” || _type == “reviewPost”] | order(_createdAt desc)

If it does, that’d be awesome. AFAIK sorting wouldn’t be possible with GraphQL unless you wrap them in a ‘Post’ type which would reference reviewPost or standardPost (really messy).
Nov 1, 2020, 4:28 PM
It looks like it should. You can try out the query using Vision (
<path-to-sanity>/vision
).
Nov 1, 2020, 4:30 PM
Thanks
user A
. I haven't actually started using Sanity (I'm considering it for this reason, actually). But from the GROQ.dev playground, it seems possible. However, I take it this would mean I'd have to stay away from Gatsby as that enforces the GraphQL API
Nov 1, 2020, 4:40 PM
I see. I decided on Sanity over a few others and have been very happy with it so far. I’ve never used this plugin personally as I haven’t built anything with Gatsby and Sanity, but this may be worth checking out: https://github.com/kmcaloon/gatsby-plugin-groq
Nov 1, 2020, 4:44 PM
Interesting. I prefer to stick with official libraries so I think I'll skip Gatsby, since I was already planning to try something on the new release of NextJS
Nov 1, 2020, 4:56 PM
Your query will work but you can simplify it like this

*[_type in ["standardPost", "reviewPost"]]
Nov 1, 2020, 5:17 PM
user J
sweet, thanks!
Nov 1, 2020, 6:24 PM

Sanity.io – build remarkable experiences at scale

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