Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting Ghost to Wordpress

I've been searching for a possibility to copy my Ghost Blog posts to Wordpress.

So far, I've managed to export all ghost data to a JSON file -- do you know any existing tool to convert it to something Wordpress can import?

If not, and I have to build something myself, would you recommend parsing the JSON to a WXR file or similar, or rather import into Wordpress' DB directly?

Thanks in advance! K.

like image 633
Karl Wolf Avatar asked Feb 06 '26 16:02

Karl Wolf


2 Answers

I migrated a Ghost blog to Wordpress by reading in the Ghost JSON export, massaging it a little and using wp_insert_post to import the posts.

This code should be placed in your theme's functions.php file - you can export your Ghost posts (GhostData.json) at http://example.com/ghost/debug/.

Note: example below doesn't import all data, but most key fields.

/**
 *  A function used to programmatically create a post in WordPress.
 *
 *  http://tommcfarlin.com/programmatically-create-a-post-in-wordpress/
 *
 *  @returns post ID if successful
 *           -1 if the post was never created
 *           -2 if a post with the same title exists
 */
function create_wp_post ($post_details) {
    // Initialize the page ID to -1. This indicates no action has been taken.
    $post_id = -1;

    $post = get_page_by_title($post_details['title'], 'OBJECT', 'post');

    // If the page title doesn't already exist, then insert the post
    if (is_null($post)) {
        // Set the post ID so that we know the post was created successfully
        $post_id = wp_insert_post(
            array(
                'comment_status'    =>  'closed',
                'ping_status'       =>  'closed',
                'post_author'       =>  $post_details['author'],
                'post_content'      =>  $post_details['content'],
                'post_date'         =>  $post_details['date'],
                'post_date_gmt'     =>  $post_details['date_gmt'],
                'post_name'         =>  $post_details['slug'],
                'post_status'       =>  $post_details['status'],
                'post_title'        =>  $post_details['title'],
                'post_type'         =>  'post',
                'tags_input'        =>  $post_details['tags_input']
            )
        );
    // Page title already exists, return error
    } else {
        $post_id = -2;
    }
}

/**
 *  A function used to filter Ghost blog posts into Wordpress format.
 */
function filter_ghost_posts () {
    $posts = json_decode(file_get_contents('GhostData.json'), true);

    if ($posts) { 
        foreach ($posts['data']['posts'] as $post) {
            $post_details = array(
                'author'        => $post['author_id'],
                'date'          => date('Y-m-d H:i:s', $post['published_at'] / 1000),
                'date_gmt'      => gmdate('Y-m-d H:i:s', $post['published_at'] / 1000),
                'id'            => $post['id'],
                'content'       => $post['html'],
                'status'        => $post['status'],
                'slug'          => $post['slug'],
                'title'         => $post['title']
            );

            // Status
            // Fix discrepancy in naming between Ghost and Wordpress
            if ($post_details['status'] === 'published') {
                $post_details['status'] = 'publish';
            }

            // Tags
            $post_tags_list = [];

            foreach ($posts['data']['posts_tags'] as $post_tags) {
                if ($post['id'] === $post_tags['post_id']) {
                    $post_tags_id = $post_tags['tag_id'];
                    array_push($post_tags_list, $posts['data']['tags'][$post_tags_id]['name']);
                }
            }

            if (count($post_tags_list) > 0) {
                $post_details['tags_input'] = implode(',', $post_tags_list);
            }

            $post_id = create_wp_post($post_details);

            if ($post_id == -1 || $post_id == -2) {
                // Error handling here
            }
        }
    } 
}

add_filter('after_setup_theme', 'filter_ghost_posts');

My recommendation would be to use Google Refine to import the JSON, and export a CSV, then use WP Ultimate CSV Importer Plugin to import it into your WordPress site. Hope this helps.

like image 35
michaelrmcneill Avatar answered Feb 09 '26 12:02

michaelrmcneill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!