Preparing your migration script
2025-11-09

The destination for migrating content from WordPress to Sanity is now ready, so we'll proceed using the migration script provided as a sample.

The official page regarding the procedure here is as follows.

If you're more comfortable with English, please refer to the official steps. This is simply the English version of the article introduced in Japanese.

Script Preparation

A base script for migrating from WordPress to Sanity is provided. Using this base script will generally allow you to migrate normally. The script will be copied when you execute the following command:

sh

When you execute the command, a prompt will appear.

  • For the Option, press Enter without specifying anything this time.
  • Please select Minimalistic migration to get you started

After the command completes, the file migrations/import-wp/index.ts will be created.

Image

To run the tool, install the components required for the environment. The command is as follows:

sh

Next, prepare the file `migrations/import-wp/types.ts` as the file defining the schema used by the migration script.

./migrations/import-wp/types.ts

Create a file named constants.ts in the ./migrations/import-wp directory to define the source site information. For the URL portion, please refer to the previous article for instructions on how to obtain it.

This time, it will be as follows.

./migrations/import-wp/constants.ts

Finally, create the file `./migrations/import-wp/lib/wpDataTypeFetch.ts` to complete the setup.

./migrations/import-wp/lib/wpDataTypeFetch.ts

The file structure in the folder under migrations upon completion is as follows:

        • wpDataTypeFetch.ts
      • constants.ts
      • index.ts
      • types.ts

Script Update

To actually run the script prepared above, update the file `./migrations/import-wp/index.ts`. The updated result is as follows.

./migrations/import-wp/index.ts

The main changes are as follows.

  • Loading wpDataTypeFetch.ts and enabling the added file.
  • wpDataTypeFetch.ts also imports constants.ts

Everything is now ready.

Execute the script

With preparations now complete, we will run the script to begin the first step of the data migration.

sh

When executed, a test will run to see if blog posts can be retrieved.

Image

問題なく取得できているので、以下のコマンドを実行して、データのインポートを実行します。

sh

When you run the command, a confirmation prompt will appear asking whether you want to proceed with importing the data. Here, we will continue by selecting Y.

Image

Once the script completes execution, the data migration will be finished. Launch Sanity Studio.

sh

Content title import complete.

Image

Summary

By following the steps to set up a script that integrates with WordPress content, I was able to migrate content to Sanity using the titles from the WordPress content. The code up to this point is published in the following branch.

Now that we've successfully migrated the titles, we can proceed to the next step.