Importing from another MySQL into WordPress
Sometimes data imports into WordPress can be a pain, but most of the time, it’s pretty darn easy. In a previous project I had to go through the trouble of outputting WXR files and then importing those, but it’s really a lot easier if you do a direct MySQL to WordPress import using the WordPress API. I did one this morning for Bergler, a client of ours, and wanted to quickly walk you through how I did it, as it’s something I guess some of you can benefit from.
The old database in this case was very very simple, I’ve renamed the column names as they were originally in Dutch, but that’s about all I changed.
So, first of all, we connect to the old database:
require("include/connect.inc"); $conn = mysql_connect($DBHost,$DBUser,$DBPassword); mysql_select_db($DBName,$conn);
The include/connect.inc file contained the variables used to connect to the database in this case.
Then we query for the news:
$results = mysql_query("SELECT * FROM news",$conn);
And then we loop through it, this is where most of the magic happens. To have the posts published immediately on import, we have to set post_status to ‘publish’. I also determined the category I wanted these posts to go into, number 4 in this case. In a lot of other cases you might have to map the original category to your new one, but that should be easy too. Finally I converted the date from a dd/mm/yyyy string to a date that WordPress understands, and then just put the appropriate fields in the old database into their new names:
$i = 0; while ($row = mysql_fetch_array($results,MYSQL_ASSOC)) { $post = array(); $post['post_status'] = 'publish'; $post['post_category'] = array(4); $post['post_date'] = date('Y-m-d H:i:s',strtotime($row['newsitem_date'])); $post['post_title'] = $row['newsitem_title']; $post['post_content'] = $row['newsitem_content']; $posts[$i] = $post; $i++; }
As you can see I’ve now created a $posts array, I could have immediately inserted the posts into WordPress here but I wanted to prevent any clashes between the two databases. So the next step is to free the results and close the connection:
mysql_free_result($results); mysql_close($conn);
Now we load up WordPress (this line is assuming you’ll be running this import script from the WordPress root directory):
require('./wp-load.php');
And finally we loop through the posts and insert them into the database:
foreach ($posts as $post) { wp_insert_post($post); }
That’s it! Easy does it. If there’s a LOT of news you might have to play with script execution times etc, but this code just imported 200+ news items in under 5 seconds for me.
Importing from another MySQL into WordPress is a post from Joost de Valk's Yoast – Tweaking Websites.A good WordPress blog needs good hosting, you don’t want your blog to be slow, or, even worse, down, do you? Check out my thoughts on WordPress hosting!

SOURCE : http://feeds.feedburner.com/joostdevalk?format=xml
FOR MORE INFORMATION PLEASE VISITE AUTHOR WEBSITE
Popularity: 1% [?]
