Get Ridiculously Organized Quickly with GROQ
GROQ (Graph-Relational Object Queries) is an incredibly powerful query language tailored for use with Sanity.io. Over time, it’s become an indispensable tool in my development toolkit, especially within the context of SanityPress or any Sanity project. Whether you're looking to filter content, project specific fields, or handle complex data structures, GROQ has you covered.
GROQ stands out among query languages for its ability to handle complex content structures effortlessly. Whether you're building a simple blog or a complex e-commerce site, GROQ gives you the tools to structure your data queries in a way that aligns with your project's needs.
For kicks and giggles, I had AI make some alternative acronyms:
- Generally Rocks Other Queries
- Grandmaster Retriever of Optimal Queries
- Get Ready for Outstanding Querying
- Great Results Obtained Quickly
🖤 Useful GROQ Queries in SanityPress🔗
Below are some of the most useful GROQ queries I’ve implemented in SanityPress. These queries are versatile and can be adapted for various content models, whether you're filtering posts by category, fetching related content, or implementing pagination.
Get all pages excluding X, Y, and Z🔗
*[
_type == 'page' &&
!(metadata.slug.current in ['index', '404', 'blog/*'])
]
Joining conditional references efficiently🔗
In SanityPress, some page modules may have fields that reference other documents. You can join those references on a per-module basis:
*[_type == 'page' && metadata.slug.current == $slug][0]{
...,
modules[]{
...,
# specify the module _type, then join the reference
_type == 'logo-list' => { logos[]-> },
_type == 'pricing-list' => {
tiers[]->{
...,
ctas[]{ ${ctaQuery} }
}
},
_type == 'testimonial-list' => { testimonials[]-> },
}
}
Processing blog content data🔗
Like calculating read time and generating table of contents for a blog post.
*[_type == 'blog.post' && metadata.slug.current == $slug][0]{
# get everything else
...,
# calculate read time
'readTime': length(pt::text(body)) / 200,
# get heading text for table of contents
'headings': body[style in ['h2', 'h3']]{
style,
'text': pt::text(@)
},
# join references for blog categories
categories[]->
}
Get all blog categories referenced by at least 1 post🔗
*[
_type == 'blog.category' &&
count(*[_type == 'blog.post' && references(^._id)]) > 0
]|order(title)
# ^ then sort by the category title
Multiple sorting conditions🔗
- Show featured posts first
- Then, sort by newest posts
- Show up to
$limit
(a number) posts
*[_type == 'blog.post']|order(featured desc, publishDate desc)[0...$limit]
📣 Shoutout to the Official GROQ Cheat Sheet🔗
While the queries I’ve shared are powerful, GROQ's full potential goes far beyond what I’ve covered. If you’re looking to dive deeper into what GROQ can do, I highly recommend checking out the official GROQ cheat sheet.
This cheat sheet is an excellent resource, offering a comprehensive overview of the language’s syntax, functions, and use cases. Whether you’re a beginner or an experienced developer, it’s a valuable tool for getting the most out of GROQ and Sanity.io.
Conclusion🔗
So there you have it—my go-to GROQ queries that keep SanityPress humming along smoothly. Whether you’re just dipping your toes into the GROQ waters or you’re ready to dive in headfirst, these queries are sure to make your content management experience a breeze.
Happy querying!