Get a peek at our latest innovations at Sanity Product Day on Dec 8th β†’

How to automatically generate slugs for a large set of articles?

6 replies
Last updated: Oct 1, 2021

I have tons of articles, but they don't have slugs. I've now added a slug field to the schema, but it looks like I have to manually go in to every article, click generate, and then publish.
Is there a quicker way?

Oct 1, 2021, 10:57 AM

Make a script that does it? You can adapt this and run

or something to set

Oct 1, 2021, 12:32 PM

const buildPatches = docs => => ({
    id: doc._id,
    patch: {
      set: {'slug.current': slugify(doc.title)},
      // ifRevisionID: doc._rev

Oct 1, 2021, 12:32 PM

could work! where can I read up on running these kinds of scripts?

Oct 1, 2021, 12:33 PM

sanity exec --with-user-token slug.js

Oct 1, 2021, 12:34 PM

ah great! thanks πŸ™‚

Oct 1, 2021, 12:40 PM Get the most out of your content is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Free to get started, and pay-as-you-go on all plans. Find out more.