Jump to content

Help with MySQL


btvbill

Recommended Posts

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);

 

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.