Converting comma-separated string to multiple tags in Sanity

12 replies
Last updated: Dec 29, 2022
Hello, I’m new to Sanity, I’ve created a Tag type that includes a slug and a title, then in the Post type I’ve added this field:

{
      name: 'tags',
      title: 'Tags',
      type: 'array',
      of: [{type: 'reference', to: {type: 'tag'}}]
    }
It works but I have to add / select tags one by one, is there any way to convert a comma-separated string to multiple tags (they are objects and it’s not a string array).

Thanks
Dec 20, 2022, 10:24 PM
Hey
user E
! If I'm understanding correctly, what you'll need to do is run a mutation. How does the comma separated string relate to your tags? Are they titles or slugs for the tags?
Dec 20, 2022, 10:58 PM
Hello
user M
, they will be title generally, if one doesn’t exist it would be good if it’s created automatically
Dec 21, 2022, 8:32 AM
Got it! You'd have to write a migration script to populate them then. Here's the basics of what you'd need to do:
const query = `{
  "tags: *[_type == "tag"]{_id, title},
  "posts": *[_type == "post"]
}`

//fetch all tag and post documents
const {tags, posts} = await client.fetch(query)

//loop through all posts
posts.forEach(post => {
  //split the string of tag titles you have and return an array of refs instead
  const tagRefs = tagString.split(',').map(title => {
    const tag = tags.find(tag => tag.title === title)
    return {_type: 'reference', _ref: tag._id}
  })

 //patch the post with the new array of tag refs
  client.patch(post._id).set({tags: tagRefs}).commit({autoGenerateArrayKeys: true,})
})
Depending on what version of the Studio you're using, you'd need to
import and configure a client. If you have a large number of documents, you'd also need to work in batches to make sure you don't hit the API rate limit . Finally, you'd execute the script
sanity exec ./path/to/script --with-user-token
Dec 21, 2022, 7:29 PM
thanks, that cannot then be done in real time and I would also have to “flag” them so I can only select the new (or edited) ones and avoid that ones that have been already tagged
Dec 27, 2022, 3:07 PM
is there another way you suggest?
Dec 27, 2022, 3:07 PM
also, is there a simpler way just to use an easier UI component that make it easy to type them one after the other and select the correct one? At the moment the interface is good for small selections but slow for things like that
Dec 27, 2022, 3:08 PM
I’m using Studio 3
Dec 27, 2022, 3:08 PM
also, what do you suggest for full text search of content?
Dec 27, 2022, 3:10 PM
If you need to run it in real time, then you'd use a client configured to use a token. This functionality doesn't exist natively in the Studio, you'd need to set it up as a custom component or plugin (which is much easier than it initially seems, I promise!).
If you search our docs for 'full text search' you'll find the docs you're looking for.
Dec 28, 2022, 5:33 PM
cool, thanks for the help
Dec 29, 2022, 4:02 PM
🙂
Dec 29, 2022, 4:02 PM
You're welcome!
Dec 29, 2022, 7:43 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.

Was this answer helpful?