WordPress Import Hack – Quick & Dirty

After finally convincing a client of mine to replace their existing 6 year old, custom built, extremely limiting & buggy blog software I’ve had to put in a bit of effort to transfer there 150+ blog posts from the MySQL table into WordPresses MySQL tables, which is pretty straight forward; the key here is to keep all of there featured images in place.

The following code does just that. It takes all the posts from the current blog, imports them into WordPresses tables, it inserts the featured image and then links it to the parent post. All thats then left to do is upload the images directory to WordPresses upload dir.

[php]
## DATABASE CONNECTION
// trololol you don’t get to see this :]

## CLEAN URLS PLEASE
function toAscii($str) {
$clean = preg_replace(“/[^a-zA-Z0-9\/_|+ -]/”, ”, $str);
$clean = strtolower(trim($clean, ‘-‘));
$clean = preg_replace(“/[\/_|+ -]+/”, ‘-‘, $clean);
return $clean;
}

## SELECT THE BLOG DATABASE TO IMPORT, ORDER IT BY ID ASC (OR BY DATE ASC W/E)
$blog_q = mysql_query(“SELECT * FROM `blog` ORDER BY `ID` ASC”);
while($blog = mysql_fetch_array($blog_q)):

## INSERT POST INTO WORDPRESS
mysql_query(“INSERT INTO `wp_posts` (
`post_author`,
`post_date`,
`post_date_gmt`,
`post_content`,
`post_title`,
`post_excerpt`,
`post_status`,
`comment_status`,
`ping_status`,
`post_password`,
`post_name`,
`to_ping`,
`pinged`,
`post_modified`,
`post_modified_gmt`,
`post_content_filtered`,
`post_parent`,
`guid`,
`menu_order`,
`post_type`,
`post_mime_type`,
`comment_count`) VALUES(
‘1’,
‘”.$blog[‘blogDATESUBMITTED’].”‘,
‘”.$blog[‘blogDATESUBMITTED’].”‘,
‘”.$blog[‘blogARTICLE’].”‘,
‘”.$blog[‘blogTITLE’].”‘,
”,
‘publish’,
‘open’,
‘open’,
”,
‘”.toAscii($blog[‘blogTITLE’]).”‘,
”,
”,
‘”.$blog[‘blogDATESUBMITTED’].”‘,
‘”.$blog[‘blogDATESUBMITTED’].”‘,
”,
‘0’,
”,
‘0’,
‘post’,
”,
‘0’
)”);

## INSERT IMAGE INTO POSTS FEATURED IMAGE
mysql_query(“INSERT INTO `wp_posts` (
`post_author`,
`post_date`,
`post_date_gmt`,
`post_content`,
`post_title`,
`post_excerpt`,
`post_status`,
`comment_status`,
`ping_status`,
`post_password`,
`post_name`,
`to_ping`,
`pinged`,
`post_modified`,
`post_modified_gmt`,
`post_content_filtered`,
`post_parent`,
`guid`,
`menu_order`,
`post_type`,
`post_mime_type`,
`comment_count`) VALUES(
‘1’,
‘”.$blog[‘blogDATESUBMITTED’].”‘,
‘”.$blog[‘blogDATESUBMITTED’].”‘,
”,
‘”.$blog[‘blogIMAGEALT’].”‘,
”,
‘inherit’,
‘open’,
‘open’,
”,
‘”.toAscii($blog[‘blogIMAGEALT’]).”‘,
”,
”,
‘”.$blog[‘blogDATESUBMITTED’].”‘,
‘”.$blog[‘blogDATESUBMITTED’].”‘,
”,
‘”.mysql_insert_id().”‘,
‘http://www.newdomain.com/wp-content/uploads/2012/03/”.$blog[‘blogIMAGE’].”‘,
‘0’,
‘attachment’,
‘image/jpeg’,
‘0’
)”);

## LINK IT TOGETHER IN THE POST_META
mysql_query(“INSERT INTO `wp_postmeta` (`post_id`, `meta_key`, `meta_value`) VALUES(‘”.mysql_insert_id().”‘, ‘_wp_attached_file’, ‘2012/03/”.$blog[‘blogIMAGE’].”‘)”);

## AND SPIT OUT ONCE DONEā€¦.
echo $blog[‘ID’].’ —> ‘.$blog[‘blogTITLE’].’
‘;

endwhile; // end, select the blog database

[/php]