Migrating content from WordPress to Sanity.io

Recently I’ve been exploring moving from WordPress to Sanity.io, a super-fast a and feature-rich headless CMS. On an unnamed (until it launches) project, I did just that. The website, which is six year old WordPress website, needed to be exported into Sanity.

The Sanity documentation recommends you use the Sanity CLI to import your data (in a JSON file) instead of using one of their client libraries. The documentation lists the pitfalls of using the client library, so with that in mind I went with writing a custom WordPress plugin that would export a JSON file that I would then use the CLI to import on my local machine.

Schema and Models

To start off, I had my Sanity Schema on my other monitor so I could always refer back to it. In the goal of making it as close as seamless as possible (there are thousands of WordPress posts), I tried to make the content match the schema rather than the schema match the content.

Using Corcel (which is super awesome and I recommend using for any WordPress development), I essentially created models for each Sanity resource. For instance, I created an employee model that matched a custom post type. In all the models, I have custom attributes that return what I’ll need for the schema for every schema item that didn’t have a 1:1 match (in the name). Every model has a method that returns a schema object.

Corcel includes a shortcode parser, which I used to parse shortcodes into custom content blocks. This by far was the most time-consuming portion and the hardest, since the content has tons of shortcodes in it that dealt with layouts.

Putting It Together

Now that I have everything basically linked, in my plugin I have an action that can be called that runs each model and pushes the returned object to a variable. At the end, the variable contains all the newly converted data and encodes it to a JSON file which I can then import.


Getting a featured image is requires you to pass the thumbnail size

Categories are a little annoying to do