CoursesMigrating content from WordPress to SanityFirst steps
Track
Replatforming from a legacy CMS to a Content Operation System

Migrating content from WordPress to Sanity

Lesson
2

First steps

Harness the power of scripting content migrations into Sanity, fix past platform mistakes, and confidently handle unique content shapes and augmentations.
Log in to mark your progress for each Lesson and Task

The first step is assessing how you export content from your WordPress project. There are several ways to extract content from WordPress. In this course, you'll target the REST API.

The WordPress REST API is a core feature of WordPress and a predictable way to query public and private content directly from the source.

If you prefer to use the XML export tooling or WPGraphQL, you must modify the logic of the scripts written in this course.

Before proceeding, you must ensure that your current install allows access to the REST API. You may have a security plugin or other configuration that blocks access. A simple way to get around this is to run WordPress locally with a copy of your production database.

Typically, you can visit the REST API at the following route:

https://<your-domain-name>/wp-json/wp/v2

You should see a JSON response in your web browser, with the available routes to query content from:

The above is viewed in a Chromium-powered web browser with the JSON Formatter extension installed

To compare, some publicly available blogs which have an open REST API include:

https://ma.tt/wp-json/wp/v2
https://blog.ted.com/wp-json/wp/v2
https://finland.fi/wp-json/wp/v2
https://www.nasa.gov/wp-json/wp/v2
Find your WordPress installation's REST API URL

Once you've confirmed your WordPress installation has an open API URL, you can get started.

For WordPress Multisite / Network setups, you will need to target a specific site by adding its name to the URL.

https://<your-domain-name>/<site-name>/wp-json/wp/v2

During the following lessons, you'll write a script that queries the URL for each content type (posts, pages, etc) individually. From your terminal, you can see how many documents of each type can be found, by using curl and returning the headers.

Run the following in the terminal, with your REST API URL:

Terminal
curl -I https://<your-domain-name>/wp-json/wp/v2/posts

You should receive a response like this, where you can see the total number of publicly available posts

Terminal
HTTP/2 200
# ...other responses
x-wp-total: 21492
x-wp-totalpages: 2150
  • x-wp-total is the number of publicly queryable posts
  • x-wp-totalpages is the number of paginated responses you can query through to retrieve them individually, using whatever posts_per_page setting is the default for your site—or added to your query. The script you write in this course will query for 100.

Now you've confirmed the URL works, and you can see how many publicly available posts can be queried from your WordPress installation, let's prepare a new home for your content.

You have 1 uncompleted task in this lesson
0 of 1