You can migrate an existing database through several means. The easiest, albeit most frustrating, method is to do it manually post by post, but let’s not entertain that idea here. Your approach will largely depend on what your data source looks like; specifically, whether you are a single author with one story, multiple stories, or multiple authors with multiple stories. The last scenario is the most challenging, aside from migrating from another CMS. Below are different methods you can try.
**IMPORTANT! ALWAYS MAKE A DATABASE BACKUP BEFORE YOU DO ANYTHING!**
Add the following script to your `functions.php` file, for example via the **Theme File Editor** under **Appearance**. This serves as the base for the described migration methods and as a template if you want to write your own script. Make sure to remove the code once you are done; you do not want to leave this in a production environment. You can find a list of all story and chapter meta fields in the [development guide](DEVELOPMENT.md#story-meta-fields).
$preview .= "<p>The following <strong>{$query->found_posts} posts</strong> will be migrated by the script. <ahref='{$perform_link}'>Start migration!</a></p>";
Add the following script to your `functions.php` file. This script will query ALL posts and convert them to chapters, optionally appending them to a prepared story post. You can customize the parameters if needed. The action can be called by opening `/wp-admin/admin-post.php?action=fictioneer_migrate` on your site. You will first get a preview of which chapters are about to be migrated; if you are happy with the preview, click the "Start migration!" link.
Add the following script to your `functions.php` file. Unlike the simple approach, this script will loop over a predefined set of author data (created by you) to migrate specific posts to specific stories. In this case, a category is used as a discriminator, but other attributes can work too. The action can be called by opening `/wp-admin/admin-post.php?action=fictioneer_migrate_by_author` on your site. Note that you will NOT get a preview; the migration happens immediately!
```php
function fictioneer_migrate_by_author() {
// Set individually by author
$story_args = [];
// Chapter query args (see https://developer.wordpress.org/reference/classes/wp_query/#parameters)
$query_args = array(
'post_type' => 'post', // Either post, page, or custom post type
'posts_per_page' => -1, // Get all posts that match the query
'post_status' => 'any', // Convert all posts regardless of status
'orderby' => 'date', // Affects the chapter order
'order' => 'asc' // Affects the chapter order
);
// Author IDs, category names, and story IDs (your responsibility to prepare this data)