Drupal 7 to WordPress content conversion SQL

As an experiment, I decided to convert content in a Drupal 7 website to WordPress.

These steps assume an empty WordPress database, with ‘admin’ as the primary user.

The first step was to insert the Posts into WordPress…

INSERT INTO dbh_wp.wp_posts
(
 post_author,
 post_date,
 post_date_gmt,
 post_content,
 post_title,
 post_excerpt
)
select 
 1,
 FROM_UNIXTIME(n.created),
 FROM_UNIXTIME(n.created),
 b.body_value,
 n.title,
 b.body_summary
FROM dbh.drupal_node n,
 dbh.drupal_field_data_body b
WHERE n.status=1 AND
 b.entity_id = n.nid;

Copy the terms / tags into wordpress terms table

INSERT INTO dbh_wp.wp_terms (name,slug)
SELECT name,name FROM dbh.drupal_taxonomy_term_data;

Insert all of those terms into the ‘post_tag’ taxonomy

INSERT INTO dbh_wp.wp_term_taxonomy
 (term_id ,taxonomy)
SELECT   term_id,'post_tag'
FROM dbh_wp.wp_terms;

Now connect the new taxonomy term IDs with the posts in the wordpress term relationships table

INSERT INTO dbh_wp.wp_term_relationships
 (object_id, term_taxonomy_id) 
SELECT 
 wpp.ID, wptt.term_taxonomy_id
FROM 
 dbh.drupal_field_revision_taxonomy_vocabulary_1 tv,
 dbh.drupal_taxonomy_term_data td,
 dbh_wp.wp_terms wpt,
 dbh_wp.wp_term_taxonomy wptt,
 dbh.drupal_node dn,
 dbh_wp.wp_posts wpp
WHERE td.tid = tv.taxonomy_vocabulary_1_tid AND
 wpt.name = td.name and 
 wptt.term_id = wpt.term_id AND
 dn.nid = tv.entity_id AND
 wpp.post_title = dn.title ;

Update the term usage count

UPDATE dbh_wp.wp_term_taxonomy wptt
SET count = (
 SELECT count(*) 
 FROM dbh_wp.wp_term_relationships wptr
 WHERE wptr.term_taxonomy_id = wptt.term_taxonomy_id
 GROUP by term_taxonomy_id
);

 

Leave a Reply

Your email address will not be published. Required fields are marked *