btvbill Posted October 14, 2011 Share Posted October 14, 2011 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); ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 14, 2011 Share Posted October 14, 2011 What do you mean "it duplicates"? There's nothing in that code that inserts anything into the DB. Quote Link to comment Share on other sites More sharing options...
btvbill Posted October 14, 2011 Author Share Posted October 14, 2011 It creates duplicate posts from whatever records are in the database. I did not write the script and the website forum that I found it on did not reply to my post. Maybe it passes something to wp-load that inserts it into the WordPress database? It does work though but if you run it twice, you get duplicate posts each time you run the script. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.