Home > blogging > Migrating from Drupal to Wordpress

Migrating from Drupal to Wordpress

March 10th, 2005

I finally decided to move my greek blog from drupal to wordpress. Since there was no migration script, I wrote a couple of sql statements that moved all posts, comments and categories from my drupal tables to the (new) wordpress 1.5 tables.

Here is the proccess in short:

WARNING!!! This may delete your DATA!!! Make sure you backup EVERYTHING before starting the procedure!!!

1. setup a fresh wordpress installation.
2. make sure term_data term_hierarchy node term_node comments (drupal tables) are in the same DB you use from WP.
3. Run the following SQL statements:
delete from weblog_wp_categories ;
delete from weblog_wp_posts;
delete from weblog_wp_post2cat ;
delete from weblog_wp_comments ;

insert into weblog_wp_categories(cat_ID,cat_name, category_nicename, category_description, category_parent) select term_data.tid, name, name, description, parent from term_data, term_hierarchy where term_data.tid=term_hierarchy.tid ;

INSERT INTO weblog_wp_posts(
ID, post_date, post_content, post_title, post_excerpt, post_name, post_modified
)
SELECT nid, FROM_UNIXTIME(created), body, title, teaser, concat('OLD',nid), FROM_UNIXTIME(changed) FROM node WHERE type='blog' OR type='page' ;

INSERT INTO weblog_wp_post2cat (post_id,category_id) SELECT nid,tid FROM term_node ;

INSERT INTO weblog_wp_comments (
comment_post_ID, comment_date, comment_content, comment_parent
)
SELECT nid, FROM_UNIXTIME(timestamp), concat('',subject, '<br />', comment), thread FROM comments ;

You should now have all your posts and comments and categories in WP. Go to the admin interface and make sure everything is in place…

Notes: This is not the perfect way to migrate. Comments are not nested in the right way. A lot of things may not work. On the other hand if, like me, made a really simple use of Drupal, this should move most of your data to WP…

Panayotis blogging

  1. March 17th, 2005 at 05:31 | #1

    You can modify the comment part of the SQL statements so that the commenter’s name, email and home page are retained instead of entering each as anonymous. Change to this:

    INSERT INTO wp_comments (
    comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_date, comment_content, comment_parent
    )

    SELECT nid, name, mail, homepage, FROM_UNIXTIME(timestamp), concat(’‘,subject, ‘
    ‘, comment), thread FROM comments ;

  2. March 17th, 2005 at 05:42 | #2

    I KISS YOU!

    Absolutely wonderful, works flawlessly (well, ok, after I poke thing to allow for the fact that I have more than one database, and that I didn’t want the ‘page’ nodes imported. But wow, lovely. Thank you.

  3. ryan
    April 6th, 2005 at 11:28 | #3

    I’m trying to decide between Drupal and WordPress right now for blogging. Why are you converting to WordPress? Why is it better than the older Drupal? I’d really like to know! Thank you.

  4. May 6th, 2005 at 20:58 | #4

    Very, very cool. Thanks for posting this, my friend. If you’re ever in Ann Arbor, MI, look me up and I’ll buy you a beer, or several beers.

  5. May 7th, 2005 at 05:34 | #5

    Thanks very much for publishing this. It saved me hours of time trying to figure out how to do this on my own. In my case, the Drupal blog was running out of a different database than the WP blog, so I had to create the Drupal post table (I hadn’t been categorizing the posts), but that was simple enough to do.

    Thanks again! Dan

  6. May 12th, 2005 at 19:43 | #6

    Thanks very much for this. I’m in the process of testing a migration from Drupal to Xoops where I’ll be using the WP plugin. Posts and categories went through with no problem. Comments import are throwing sql errors, so I’ve got a bit of figuring to do yet.

  7. March 11th, 2006 at 04:58 | #7

    Here’s a cleaned up and more complete set of queries. My drupal database is called “blog” and my wordpress database is called “wordpress”. Also, I’m at -0800 to GMT.

    –Wipe out existing content
    DELETE FROM wordpress.categories;
    DELETE FROM wordpress.posts;
    DELETE FROM wordpress.post2cat;
    DELETE FROM wordpress.comments;
    DELETE FROM wordpress.users
    WHERE ID > 1;

    –Copy users
    INSERT INTO wordpress.users (ID, user_login, user_pass, user_nicename,
    user_email, user_registered, display_name)
    SELECT uid, name, pass, name, mail, FROM_UNIXTIME(created), name
    FROM blog.users
    WHERE uid > 1;

    –Copy over categories
    INSERT INTO wordpress.categories
    (cat_ID, cat_name, category_nicename, category_parent, category_count)
    SELECT term_data.tid, name, name, parent, count(term_node.tid)
    FROM blog.term_data
    INNER JOIN blog.term_hierarchy ON (term_data.tid=term_hierarchy.tid)
    LEFT JOIN blog.term_node ON (term_node.tid = term_data.tid)
    GROUP BY term_data.tid

    –Copy over all the blog posts, pages and forum topics
    –All will be posts in wordpress
    INSERT INTO wordpress.posts
    (ID, post_author, post_date, post_date_gmt,
    post_content, post_title, post_excerpt, post_status, comment_status,
    ping_status, post_name, post_modified, post_modified_gmt)
    SELECT nid, uid, FROM_UNIXTIME(created), FROM_UNIXTIME(created + (60*60*8)),
    body, title, teaser, ‘publish’, ‘open’, ‘closed’,
    concat(’node-’,nid),
    FROM_UNIXTIME(changed), FROM_UNIXTIME(changed + (60*60*8))
    FROM blog.node
    WHERE type IN (’blog’, ‘page’, ‘forum’);

    –Copy the post to category
    INSERT INTO wordpress.post2cat (post_id, category_id)
    SELECT nid,tid
    FROM blog.term_node ;

    –Copy the comments and associate them with the top level blog post (threading lost)
    INSERT INTO wordpress.comments
    (comment_ID, comment_post_ID, comment_date, comment_date_gmt, comment_content, comment_approved, comment_parent)
    SELECT cid, nid, FROM_UNIXTIME(timestamp), FROM_UNIXTIME(timestamp + (60*60*8)),
    concat(”, subject, ”, comment), 1, CONVERT(thread, UNSIGNED)
    FROM blog.comments;

  8. Jim Brown
    May 9th, 2006 at 04:28 | #8

    ALWAYS GRATEFUL!

    You saved me literally hours and hours of gruelling, repetitious work. Your work does not go unnoticed!

  9. May 16th, 2006 at 00:57 | #9

    It worked like a charm, except for the comment count part.

    Please use this

    UPDATE wp_posts, node_comment_statistics SET wp_posts.comment_count = node_comment_statistics.comment_count WHERE wp_posts.ID=node_comment_statistics.nid;

    Cheers

  10. May 20th, 2006 at 05:36 | #10

    I rewrote some SQL lines in my blog (click my name) so that this will work for Drupal 4.7. I decided to convert as well.

  1. March 29th, 2005 at 07:59 | #1
  2. May 26th, 2005 at 07:07 | #2
  3. August 12th, 2005 at 16:56 | #3
  4. September 15th, 2006 at 06:12 | #4
  5. April 9th, 2007 at 10:46 | #5
  6. May 16th, 2007 at 05:04 | #6
  7. June 7th, 2007 at 04:11 | #7
  8. July 3rd, 2007 at 02:15 | #8
  9. January 6th, 2009 at 17:54 | #9
  10. January 6th, 2009 at 23:37 | #10