👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Tips for migrating from WordPress to Sanity, including using WPGraphQL and block tools.

28 replies
Last updated: Mar 10, 2021
anyone have a successful migration from wordpress to sanity? willing to share your process?
Mar 1, 2021, 6:22 PM
i think there were some other guide on that posted here (though the history doesn’t go back far enough) but our site has a lot of ACF and Woo so we’ve been using WPGraphQL for migrations. It’s not perfect though. in particular, you can’t pull drafts or private posts from WPGraphQL
Mar 1, 2021, 8:03 PM
are you saying you didn’t migrate the content? you just also request it alongside sanity content?
Mar 1, 2021, 8:16 PM
oh i used WPGraphQL to pull content and write it in to Sanity
Mar 1, 2021, 8:17 PM
ahhh
Mar 1, 2021, 8:17 PM
interesting
Mar 1, 2021, 8:17 PM
this primarily because im not a WordPress dev so maybe this convoluted lol
Mar 1, 2021, 8:17 PM
haha
Mar 1, 2021, 8:17 PM
so then you did mutations in sanity to create/save imported pages?
Mar 1, 2021, 8:18 PM
Mar 1, 2021, 8:19 PM
but it’s definitely not a 1-to-1 transform and we’re still in the middle of auditing our pages
Mar 1, 2021, 8:19 PM
by hand
Mar 1, 2021, 8:20 PM
got it
Mar 1, 2021, 8:20 PM
but at least this gets the content partially on there
Mar 1, 2021, 8:20 PM
yeah, the use case i have right now is only a small blog with about ~100 posts
Mar 1, 2021, 8:20 PM
so not too terrible
Mar 1, 2021, 8:20 PM
if it’s only blog posts, then i think the wordpress REST API would be helpful too
Mar 1, 2021, 8:21 PM
along the same path? just query into sanity to create mutation?
Mar 1, 2021, 8:22 PM
yeah for your use case, i would go with the rest API because i hate plugins lol
Mar 1, 2021, 8:23 PM
haha… same!
Mar 1, 2021, 8:23 PM
other than ACF
Mar 1, 2021, 8:23 PM
ACF is the Sanity of WordPress
Mar 1, 2021, 8:23 PM
agree!
Mar 1, 2021, 8:24 PM
do you have any code samples you could share of your mutations? I honestly haven’t worked with them yet
Mar 1, 2021, 8:24 PM
i don’t atm but it would go something like this:

import SanitySchema from '@sanity/schema';
import blockTools from '@sanity/block-tools';
import Sanity from '@sanity/client';
import { JSDOM } from 'jsdom';

const contentType = SanitySchema.compile({
    name: 'default',
    types: [
        {
            name: 'blocks',
            type: 'array',
            of: [{ type: 'block' }],
        },
    ],
}).get('blocks');

const htmlToBlocks = (html: string) => {
    return blockTools.htmlToBlocks(html, contentType, {
        parseHtml: (html) => new JSDOM(html).window.document,
    });
};

const sanity = Sanity({/* init sanity client */});

async function main() {
  const allPosts = await getAllWordPressPostsViaTheRestApi();

  const t = sanity.transaction();

  for (const post of allPosts) {
    t.createOrReplace({
      _id: post.id.toString(),
      content: htmlToBlocks(post.content.rendered),
    });
  }

  await t.commit();
}

main().catch(e => {
  console.error(e);
  process.exit(1);
});
Mar 1, 2021, 8:30 PM
i haven’t tested the above but i think it gives the idea
Mar 1, 2021, 8:35 PM
awesome. thanks
Mar 1, 2021, 8:35 PM
i’ll definitely check it out
Mar 1, 2021, 8:35 PM
Apologies if this is old hat but
user Y
’s demo, while it hasn't been updated in some time, may be helpful!
https://github.com/kmelve/wordpress-to-sanity
Mar 10, 2021, 12:42 AM

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?