Tips for migrating from WordPress to Sanity, including using WPGraphQL and block tools.
Yes! Migrating from WordPress to Sanity is definitely doable, and there's actually a full course on this in the Sanity documentation. Here's what a typical process looks like:
General Migration Approach
The key thing to understand is that migration isn't just about moving data—it's an opportunity to restructure your content for the better. You can finally escape WordPress's "presentation lock-in" and create more reusable, structured content.
The Step-by-Step Process
1. Prepare Your Sanity Schema
Start by creating your schema types in Sanity Studio. This gives you immediate visual feedback as you migrate and lets you collaborate with your content team on the new editorial experience. You can restructure things here—for example, converting WordPress pages with different templates into more appropriate document types (staff profiles → person documents, office locations → location documents).
2. Use the Sanity CLI Migration Tool
The recommended approach is using Sanity's CLI:
npx sanity@latest migration createThis gives you built-in abstractions, automatic batching to avoid rate limits, dry-run mode by default, and document validation against your schema.
3. Start Simple and Build Incrementally
Don't try to migrate everything perfectly in one go. Start with just _id and _type fields, then add slugs, titles, and other fields in subsequent runs. This incremental approach with real-time Studio feedback creates an effective development workflow.
4. Convert HTML to Portable Text
This is where @sanity/block-tools comes in. Install it with:
npm install -D @portabletext/block-tools jsdomThe htmlToBlocks function handles basic formatting automatically, but you can define custom deserialization rules for complex HTML structures—like extracting data from <figure> or <img> tags and transforming them into custom block types.
5. Handle Assets Performantly
Uploading images requires special attention:
- Don't use sequential loops (way too slow)
- Implement concurrent uploads with rate limiting using tools like
p-limit - Fetch assets from URLs and upload using Sanity Client
- Attach the returned asset document IDs as references in your documents
6. Make Your Scripts Idempotent
Your migration scripts should be idempotent—meaning you can run them multiple times with the same result. Use deterministic IDs so scripts can skip or intentionally rewrite documents. This is crucial because you'll likely run migrations multiple times throughout the project.
7. Handle Errors Gracefully
Legacy WordPress content is often messy—missing images, corrupted content, broken references. Every retrieval must handle cases where content is corrupted or references missing elements.
Key Resources
The full WordPress to Sanity migration course walks through the entire process with practical examples. There's also a comprehensive guide on general migration principles that applies to any content migration.
Pro Tips
- Use TypeScript types generated from your schemas for type-safe transformations
- Run migrations in dry-run mode first before executing against production
- Complex page builder outputs (Elementor, Divi) can be challenging—sometimes it's better to start fresh with those
- Preserve important metadata like creation dates, authors, and taxonomies during migration
The migration is definitely worth it—you'll end up with cleaner, more queryable content that's not locked into any particular presentation format.
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.