Need help with a PHP script for WordPress post creation from MySQL

The following script creates a WordPress post directly from a MySQL database but the problem is that if you run it again, it duplicates. I’m a bit new at this and need some help with MySQL code that will update existing records in content in the database changes, otherwise ignore it and insert any new records. I’ve tried adding some query code to the script but so far have been unsuccessful. Any help would be appreciated. Here’s the script:

<?php // Configuration info goes here: this is the db I'm pulling from. $hostname="localhost"; $dbname="testwp"; $user="root"; $pass=""; // Update the category # below inside the array. $link = mysql_connect($hostname, $user, $pass); mysql_select_db($dbname, $link); $results = mysql_query("SELECT * FROM genesis",$link); require('./wp-load.php'); $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['date'])); $post['post_title'] = $row['title']; $post['post_content'] = $row['description']; $posts[$i] = $post; wp_insert_post($post); $i++; } mysql_free_result($results); mysql_close($link); ?>

have you looked to see if there an error:

[php]<?php wp_insert_post( $post, $wp_error ); ?>[/php]

http://codex.wordpress.org/Function_Reference/wp_insert_post

Sponsor our Newsletter | Privacy Policy | Terms of Service