[wp-trac] [WordPress Trac] #23839: wp_insert_post() duplicate insertions

WordPress Trac noreply at wordpress.org
Fri Mar 22 10:23:48 UTC 2013


#23839: wp_insert_post() duplicate insertions
----------------------------+------------------------------
 Reporter:  mikemayhem3030  |       Owner:
     Type:  defect (bug)    |      Status:  new
 Priority:  normal          |   Milestone:  Awaiting Review
Component:  General         |     Version:  3.5.1
 Severity:  normal          |  Resolution:
 Keywords:  close           |
----------------------------+------------------------------

Comment (by mikemayhem3030):

 Hello

 How can you be that sure I've done something wrong?

       $j=0;

       foreach($pictures as $picture){
                 $the_img = wp_get_attachment_image_src(
 $picture->ID,"full" );

                 $title = get_post_meta($picture->ID,
 '_wp_attached_file',true);
                 if (!get_page_by_title($title, 'OBJECT', 'picsmash') ){
                              $my_post = array(
                              'post_title' => $title,
                              'post_status' => 'publish',
                              'post_type' => 'picsmash',
                               );

                             #} Insert the post into the database
                              $post_id = wp_insert_post( $my_post );


                             }
                         $j++;


                 }


 That's my code. If I output $j it is outputting the correct number of
 pictures, however when I then view the custom post type - it has double
 the number of posts.

 It seems to be caused by the fact this is in a function, and I run it
 using AJAX.

 I would imagine if I change it so it's not AJAX related it would probably
 work OK. But it's not as slick that way.

 Cheers

 Mike

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/23839#comment:2>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list