I'm trying to programatically insert posts from an external MySQL database as posts on my wordpress blog, I've got something that works, but there is no way of wordpress checking to see if the post already exists before it inserts a new post at the moment, so I'm getting lots of duplicate entries.
I'm sure for anyone who knows how, we're talking about some simple conditional if, else statements here, but I can't get my head around what to check for to make sure that the post hasn't already been added to the wordpress database? I've figured its something of a distinction between wp_insert_post and wp_update_post, I just can't figure out what to check against and how I get wordpress to check each entry?
Do I need to add some custom_meta data? Or can the conditional statement check against the title or date for example?
Really appreciate any help of advice anyone can suggest?
Thanks a lot!
My code is as follows:
if( ) {
// Create post object
$post = array();
$post['post_title'] = $user ." said " . $text;
$post['post_content'] = "<div class='tweet'><a href='" . $user_url . "'><img class='photo' src='" . $image . "' alt='" . $user . "' /></a><a href='" . $user_url . "'>" . $user . "</a> <strong>" . $date .": </strong><p>" . $text . "</p></div>\n";
$post['post_status'] = 'publish';
$post['post_date'] = date("jS F Y, G:i.s", $date);
//$post['post_type'] => 'post';
// Insert the post into the database
wp_insert_post( $post );
}
else {
// Update post object
$post = array();
$post['post_title'] = $user ." said " . $text;
$post['post_content'] = "<div class='tweet'><a href='" . $user_url . "'><img class='photo' src='" . $image . "' alt='" . $user . "' /></a><a href='" . $user_url . "'>" . $user . "</a> <strong>" . $date .": </strong><p>" . $text . "</p></div>\n";
$post['post_status'] = 'publish';
$post['post_date'] = date("jS F Y, G:i.s", $date);
//$post['post_type'] => 'post';
// Insert the post into the database
wp_update_post( $post );
}