How to Format References with Types of Posts
2 replies
Last updated: Apr 1, 2021
M
Hey guys, got a question.
I have an array which has references to different types of documents; for example, there’s a
For example, this query gives me the following output:
This is just a straight output of all the references being followed. If I want to format references with
Thanks!
I have an array which has references to different types of documents; for example, there’s a
Postsarray which contains references to different types of posts such as
merchPost,
classPost,
announcementPost, etc. I want to query for this
Postsarray, and then follow the references for each of the elements so that I can get expanded content for all these different types of posts. The issue I’m having trouble figuring out is how can I format each type of reference individually?
For example, this query gives me the following output:
----- QUERY *[ _type == "collectionFeedPosts" && slug.current == "feed-posts" ][0] { title, "pinnedPosts": pinnedPosts[]-> } ----- RESULT { "pinnedPosts": [ { "_createdAt": "2021-04-01T16:33:37Z", "_id": "aea7a497-7d50-45b4-af77-5f3091e570b7", "_rev": "p5oiLzuoOfR9bp21r2Rtt7", "_type": "merchContentPosts", "_updatedAt": "2021-04-01T16:33:37Z", "body": [{...}], "merchLink": "<http://www.url.com|www.url.com>", "slug": { "_type": "slug", "current": "test-merch-1" }, "title": "test merch 1" }, { "_createdAt": "2021-04-01T16:32:52Z", "_id": "20c66c4c-2ba9-4786-a923-4ccd19958a1a", "_rev": "TDo1Nq1xkiZakmRW4o2VqB", "_type": "mashupClassPosts", "_updatedAt": "2021-04-01T16:32:52Z", "body": [{...}], "slug": { "_type": "slug", "current": "test-class-1" }, "title": "test class 1", "video": {...} }, { "_createdAt": "2021-03-31T22:26:24Z", "_id": "5c723ad4-4ee9-4eeb-9cb5-c96195f9ad92", "_rev": "p5oiLzuoOfR9bp21r1YQuj", "_type": "featureAnnouncementPosts", "_updatedAt": "2021-03-31T22:26:24Z", "launchedOn": "2021-03-31T22:00:00.000Z", "slug": { "_type": "slug", "current": "test-1" }, "title": "test 1" } ], "title": "Feed Posts" }
_type == "featureAnnouncementPosts"differently than references with
_type == "merchContentPosts"(f.e maybe
merchContentPostsshould have the “slug” under a different key name), how would I go about doing this?
Thanks!
Apr 1, 2021, 10:25 PM
G
Hi Mannan. If you look at Conditionals on the Query Cheat Sheet, the bottom example (with the comment
// Specify sets of projections for different content types …) sounds like it might be what you’re after.
Apr 1, 2021, 10:36 PM
M
Awesome, I think that’s it. Thanks!
Apr 1, 2021, 10:44 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.