No subject


Wed Aug 17 18:42:36 UTC 2011


the same record to be inserted multiple times.

To remedy the situation, I've tried 3 different techniques including
the use of custom fields, but no luck. I also tried inserting a unique
key into the post itself to minimize the database activity. I found
this is better cause it avoids the involvement of custom fields, and
yet achieves the same goal. I put the source_key ( which is something
like --{$db_name}.{$table_name}.{$recid_value}-- ) in the post and
at_insert_time , I just check the existence of that key ( using like
operator ) to see if that post was added previously. but even that
fails... Wp_insert_post surprisingly creates double records. and up
until now, I simply cannot pin-down the occurrence of the problem.

I read this, Prevent duplicate posts in wp_insert_post using custom
fields But my techniques were simply alternatives to it.

Here is a cut-down code of how I do it...

while ($row = mysql_fetch_assoc($RS)) :

        $source_recid = $row['source_recid'];
        //source_recid is something like db.table.tablerecid
            //example services.media.1223

        $post_data['post_content'] = $row['some_field_thats_got_page_content'];

        //append the source_recid to the post data in forms of an html
comment for uniqueness purposes
        $post_data['post_content'] = $post_data['post_content'] .
"<!--{$source_recid}-->";

        //do the other stuff... ( shortened here for the clarify purposes... )

        ....

        $post_data['post_status'] = 'publish';

        Insert_or_UpdatePost($dbh,$post_data,$source_recid,$post_id);


        if ($post_id==0):

            //log the error and move on

            continue;

        endif;


endwhile;

function Insert_or_UpdatePost($dbh,$post_data,$source_recid,&$post_id){

        // this function first looks up the --db.table.tablerecid-- sequence
        // across all wp_posts post_content field
        // in determining if this record has been already INSERTed!

        // and depending on the outcome, it does an insert or update
        // and return the $post_id accordingly

        // if the function determines there are no such records,
        // then and only then, it runs the wp_insert_post!

        // if the function determines that there is a post like that,
        // it retrieves the post_id
        // and then switches to operation
        // to use the wp_update_post instead!

        // with this approach, technically speaking,
        // it should impossible to run wp_insert_post on an existing post!

        // and yet, I still get duplicate posts...

        // here we go;

        $post_id_probed = blp_sql_getdbvals($dbh,"select id from
wp_posts where post_content LIKE '%--{$source_recid}--%'");

        if (blp_isnumber($post_id_probed)):
            $post_id = $post_id_probed;

            $post_data['ID'] = $post_id;
            $post_id = wp_update_post( $post_data );

            if ($post_id == 0):

                //add error

                return FALSE;


            else:

                update_post_meta($post_id, "wpcf-blp-migration-date",
blp_now('mysql'));

                return TRUE;

            endif;


         endif;

         // if we make it this part, it means only one thing!
         // there is no post for the db.table.tablerecid yet,
         // so do the insert!

        $post_id = wp_insert_post( $post_data );



        if ($post_id == 0):

            //add error

            return FALSE;


        else:

            //add_post_meta($post_id, "wpcf-blp-migration-source",
$source_recid,TRUE);
            //no need for that anymore

            return TRUE;


        endif;


}

There is also a post I created at wordpress.stackexchange site.

http://wordpress.stackexchange.com/questions/19732/prevent-duplicate-posts-in-wp-insert-post-using-custom-fields


More information about the wp-hackers mailing list