How to Migrate WordPress Content to Sanity, Step by Step
How to migrate WordPress content to Sanity: audit, export, model content, convert HTML to Portable Text, move images and verify before launch.
6 min read

On this page
To migrate WordPress content to Sanity, you export your posts, pages and media from WordPress, model the same content as Sanity document types, convert the HTML body into Sanity's Portable Text, upload the images as Sanity assets, and import everything with a script you can safely re-run. Then you check the result against your old site before anything goes live. The work is mostly planning and cleanup, not copying.
Here is the process I follow when I move a WordPress site to Next.js with Sanity as the CMS.
What does it mean to migrate WordPress content to Sanity?
WordPress stores your content in a database as posts, pages, custom post types, categories, tags and media files. Sanity is a headless CMS that stores content as structured JSON documents, which you define with a schema, and your Next.js site reads them through an API.
So this is not a file transfer. Each WordPress item has to be translated into a Sanity document with typed fields. A blog post becomes a post document with a title, slug, date, author reference, cover image and a rich text body.
Step 1: Audit what is actually on the WordPress site
Before exporting anything, list what you have. In projects like this, I usually go through:
- Post types: posts, pages and any custom post types from plugins or themes.
- Taxonomies: categories, tags and custom taxonomies.
- Custom fields: for example Advanced Custom Fields data, or SEO fields from an SEO plugin.
- Media: how many files, and whether old uploads are actually used.
- Shortcodes and page-builder content: these do not convert cleanly.
- Content nobody needs: old drafts, test pages, thin posts.
Decide what to leave behind. Migrating less content is cheaper, faster and gives you a cleaner site.
Step 2: How do you export content from WordPress?
You have two realistic options.
The built-in export file
In the WordPress admin, Tools > Export produces an XML file (WXR) with posts, pages, comments, categories, tags and custom fields. It is simple, but media files are only referenced by URL, and some plugin data may be missing.
The WordPress REST API
Most sites expose posts and pages as JSON at /wp-json/wp/v2/posts and /wp-json/wp/v2/pages, with media at /wp-json/wp/v2/media. The API is paginated, and custom field data only appears if the plugin exposes it. I prefer the API for repeatable scripts, and the export file as a cross-check.
Either way, take a full backup of the site first.
Step 3: How should you model the content in Sanity?
This is the step that decides how pleasant the CMS will be for your editors. Do not copy WordPress's structure blindly.
- Turn repeated things into their own document types. Authors, categories and testimonials become documents that posts reference, instead of plain text typed again each time.
- Replace one big content blob with fields. If every service page has a headline, intro, benefits list and call to action, give those their own fields.
- Keep SEO fields explicit: meta title, meta description and an optional social image on each page type.
- Keep the old slug as a field so URLs can be preserved or redirected. This protects your rankings; redirects for any URL that changes matter just as much.
Then build the Sanity Studio so editors see clear labels and sensible groups. A well-modelled Studio is often what makes the move worth it.
Step 4: How do you convert WordPress HTML to Portable Text?
WordPress body content is HTML, often with Gutenberg block comments, shortcodes and inline styles. Sanity's rich text format is Portable Text, a JSON structure of blocks and spans.
Sanity provides tooling to convert HTML into Portable Text, and you can add rules for custom elements such as images, embeds, buttons or tables. In practice:
- Convert headings, paragraphs, lists, links, bold and italic automatically.
- Write custom rules for images, videos and embeds, mapping each to a block type you defined in the schema.
- Strip inline styles and empty paragraphs, which are common in older WordPress content.
- Handle shortcodes and page-builder markup individually. Some become structured fields; some are rewritten by hand.
Expect a small share of pages to need manual attention. Check these with a script that flags unusual content, such as leftover shortcode brackets or unknown tags.
Step 5: How do you move WordPress images to Sanity?
Images in the body are URLs pointing at your old uploads folder. If you leave them there, the new site depends on WordPress hosting forever.
Instead, download each file, upload it to Sanity as an asset, and replace the old URL with a reference to the new asset. Sanity serves images through its own image pipeline, so Next.js can request correctly sized versions. Add alt text during this step, because many old images have none. Keep a mapping of old URL to new asset so a re-run does not upload duplicates.
Step 6: Import the content and keep the script re-runnable
Write the migration as a script that creates documents with predictable IDs, for example derived from the WordPress post ID. That way running it again updates documents instead of duplicating them. Sanity's CLI can also import a file of documents in newline-delimited JSON.
Run it first against a test dataset, never your production one. You will run it several times while fixing edge cases, and a re-runnable script makes that painless.
Step 7: How do you check the migrated content?
Compare the old and new sites systematically:
- Count documents per type against the audit.
- Spot-check your top pages by traffic and your highest-converting pages.
- Check images, links, headings and tables on migrated pages.
- Confirm titles, meta descriptions and slugs match the old ones.
- Crawl the old URL list and confirm every URL either exists or redirects.
Fix the issues in the script rather than editing content by hand, then re-run it. Once the content is approved, run the final import just before launch.
What can go wrong?
- Plugin data nobody mentioned. Form entries, memberships or WooCommerce orders are not blog content and need a separate plan.
- Broken internal links. Posts often link to each other by full URL, so rewrite them during conversion.
- Content added during migration. Agree a freeze date, or run a final delta import.
- Ecommerce. A store is a different project; moving it to Next.js needs a separate decision on the shop platform.
If you want help with this, my WordPress to Next.js and Sanity migration service covers content, media, redirects and metadata checks.
FAQ
Can I import a WordPress XML export directly into Sanity?
Not as-is. The WXR file has a different structure from Sanity documents, so you need a script that reads it, maps fields to your schema and converts the body to Portable Text. The XML is still useful as a source or a cross-check.
Will my images and media be moved too?
Only if you migrate them deliberately. Media files must be downloaded and uploaded to Sanity as assets, and the references in your content rewritten. Otherwise the new site keeps loading images from the old WordPress server.
Do I lose my SEO when I migrate content to Sanity?
Not if you keep the same URLs or redirect each old URL to its new equivalent, and carry over titles, meta descriptions and headings. Rankings can still fluctuate for a while after launch, so monitor Search Console closely.
How long does a WordPress to Sanity content migration take?
It depends on how much content you have, how many custom fields and page-builder pages, and how much cleanup is needed. A small blog is far quicker than a large site with many custom post types, so I give a fixed quote only after seeing the actual site.
Key takeaways
- Audit first and migrate only what you need.
- Model content as structured Sanity documents rather than copying WordPress's layout.
- Convert HTML to Portable Text with custom rules, and move images into Sanity as assets.
- Use a re-runnable script, test on a separate dataset, and verify URLs and metadata before launch.
Tags
- Sanity CMS
- WordPress Migration
- Next.js
- Tutorials

About Keshur Gojiya
Web developer and HTML email specialist with 6+ years of experience, building websites and emails for businesses, startups and agencies.
Get to know me

