Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 );
             }
share|improve this question
add comment

1 Answer

What sort of unique data are returned by your external SQL query? I would assume that you would need to insert some sort of unique data (e.g. primary key from the external DB) as either $post data or metadata, and then your conditional could look for the existence of these unique data.

Alternately, you could hash the postdata from the external DB, and then store the hash value as $post metadata, and query on the existence of the hash value?

EDIT:

To query for specific $post metadata, use the get_post_custom() function (Codex ref). e.g.:

$mypostcustom = get_post_custom( $post->ID );

if ( ! isset( $mypostcustom['unique_query_data'] ) ) {
     // Unique query data not found; do something
} else {
     // Unique query data found; do something else
}

Note: this assumes that you've already worked out how to add the custom post meta, e.g. via add_post_meta() (Codex ref).

share|improve this answer
    
Hi Chip, Thanks for this, yes each entry as a unique id so I could store that as $post metadata, that's straightforward enough I think! What process do you suggest for querying it's existance, this is where my understanding gets a bit cloudy!! –  user4769 Apr 19 '11 at 17:05
    
I've just added some example code using get_post_custom(). –  Chip Bennett Apr 19 '11 at 19:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.