Querying for images by tag using GROQ in Sanity.io
6 replies
Last updated: Sep 11, 2021
E
Hello, Nice to be here. I have a question about Media Browser. specifically about https://www.sanity.io/plugins/sanity-plugin-media .I have some tags and images attached to tags. I wonder is there a way to get all the images of a tag? name and url using GROQ?
Sep 11, 2021, 6:21 PM
Hi Eran. Welcome! Would you be looking for a tag by name in your query or would you be passing in the tag’s
_id?
Sep 11, 2021, 6:32 PM
E
Hi, If I can ask for all images that has tags that would be great, otherwise by
_idis also great
Sep 11, 2021, 7:13 PM
Something like this would query for all tags ()—or specifically, return the
You might want to hide any tags that have no images; you can do that by adding
*[_type == 'media.tag']), and then for each tag, return a list of images that reference that tag (
'images': *[_type == 'sanity.imageAsset' && references(^._id)]
originalFilenameand `url`:
*[_type == 'media.tag'] { 'images': *[_type == 'sanity.imageAsset' && references(^._id)]{originalFilename, url} }
[count(images) > 0]after the projection.
*[_type == 'media.tag'] { 'images': *[_type == 'sanity.imageAsset' && references(^._id)]{originalFilename, url} }[count(images) > 0]
Sep 11, 2021, 7:32 PM
Something like this would query for all tags ()—or specifically, return the
You might want to hide any tags that have no images; you can do that by adding
*[_type == 'media.tag']), and then for each tag, return a list of images that reference that tag (
'images': *[_type == 'sanity.imageAsset' && references(^._id)]
originalFilenameand `url`:
*[_type == 'media.tag'] { 'images': *[_type == 'sanity.imageAsset' && references(^._id)]{originalFilename, url} }
[count(images) > 0]after the projection.
*[_type == 'media.tag'] { 'images': *[_type == 'sanity.imageAsset' && references(^._id)]{originalFilename, url} }[count(images) > 0]
Sep 11, 2021, 7:32 PM
E
Amazing 👍 out of curiosity, I tried to find the metadata needed for this operation (for example the
also, not related just to confirm. the Public mode of a dataset is read-only right? users cannot write into the dataset?
sanity.imageAssettype and the fields it has), but it is hard to find it in the documentation. Is there a place where you explore and play with the dataset content? like you have in graphQL?
also, not related just to confirm. the Public mode of a dataset is read-only right? users cannot write into the dataset?
Sep 11, 2021, 9:24 PM
A few things can be inferred from here and the properties are mentioned in passing here , but I agree this could be much more explicitly stated. We’ll make sure to improve those docs. Thanks for pointing that out.
For a GROQ playground, the local studio should come with the Vision plugin by default (
http://localhost:3333/vision ). If it’s not installed, the command is listed here or you can follow up in this thread and we’d be happy to help.
You’re correct: No one can write to the dataset—whether it’s public or private—without a write token.
Edit: I realize my first paragraph might not answer your question, which seems more to be asking “how do you know what you don’t know?” If that’s the case, maybe a better answer would have been to recommend you use
For a GROQ playground, the local studio should come with the Vision plugin by default (
http://localhost:3333/vision ). If it’s not installed, the command is listed here or you can follow up in this thread and we’d be happy to help.
You’re correct: No one can write to the dataset—whether it’s public or private—without a write token.
Edit: I realize my first paragraph might not answer your question, which seems more to be asking “how do you know what you don’t know?” If that’s the case, maybe a better answer would have been to recommend you use
Ctrl + spacein the Vision plugin. That should give you a dropdown selection including all the documents, objects, arrays, etc. from your schema. It’s not perfect, but it does include the
sanity.*objects.
Sep 11, 2021, 10:19 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.