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

WordPress Trac noreply at wordpress.org
Fri Mar 22 19:15:45 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:  reporter-feedback  |
-------------------------------+------------------------------

Comment (by mikemayhem3030):

 Replying to [comment:6 alexvorn2]:
 > related = #11081

 The related ticket I couldn't see how I could use this to help prevent
 what's happening. It seems pretty random (i.e. it doesn't duplicate them
 every single time, but most times it will).

 PHP handler function:-

 {{{
 function picsmash_get_all_pics(){

             global $wp, $wpdb;

             $j = 1;
             wp_reset_query();
             $querystr = "SELECT DISTINCT * FROM $wpdb->posts WHERE
 post_status != 'trash' AND post_type = 'attachment' AND (post_mime_type =
 'image/jpeg' OR post_mime_type = 'image/gif' OR post_mime_type =
 'image/png')";
             $pictures = $wpdb->get_results($querystr);

             foreach($pictures as $picture){

                 $the_img = wp_get_attachment_image_src(
 $picture->ID,"full" );
                 $img_full = $the_img[0];
                 $ID = $picture->ID;

                 $title = get_post_meta($picture->ID,
 '_wp_attached_file',true);

                                 $querystr = "SELECT ID FROM
 $wpdb->postmeta WHERE meta_value = $title";
                         $added = $wpdb->get_col($querystr);

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


                 }



             $result['message'] = $j . " new images added to the Pics
 Mash</b>";

             echo $result['message'];

             die();

 }


 add_action('wp_ajax_picsmash_get_all_pics', 'picsmash_get_all_pics');
 }}}


 JS request:-

 {{{
         $('#ps-ajax').submit(function(){
                 $('#feedback').html('<div class="loading"><img src="' +
 window.loadingImg + '" alt="" title="Adding pics" /><br />Adding
 pics...</div>').fadeIn(1000);
                         data = {
                         action: 'picsmash_get_all_pics'
                         };
                         $.post(ajaxurl, data, function(response){
                         $('#feedback').html(response);
                 });
                 return false;
         });
 }}}

 Post type reg:-
 {{{
     #} Custom post types - pics
     $labels = array(
                 'name' => _x('Pics Mash', 'post type general
 name','PicsMash'),
                 'singular_name' => _x('Pics Mash', 'post type singular
 name','PicsMash'),
                 'add_new' => _x('Manually Add Pic', 'pic','PicsMash'),
                 'add_new_item' => __('Manually Add New Pic','PicsMash'),
                 'edit_item' => __('Edit Pic','PicsMash'),
                 'new_item' => __('New Pic','PicsMash'),
                 'view_item' => __('View Pic','PicsMash'),
                 'search_items' => __('Search Pics','PicsMash'),
                 'not_found' =>  __('No pics found','PicsMash'),
                 'not_found_in_trash' => __('No pics found in
 Trash','PicsMash'),
                 'parent_item_colon' => '',
                 'menu_name' => 'Pics Mash'
             );
     $args = array(
                 'labels' => $labels,
                 'public' => true,
                 'publicly_queryable' => true,
                 'show_ui' => true,
                 'show_in_menu' => true,
                 'query_var' => true,
                 'rewrite' => array( 'slug' => 'picsmash','with_front' =>
 FALSE ),
                 'capability_type' => 'post',
                 'has_archive' => true,
                 'hierarchical' => false,
                 'menu_icon' => plugins_url('/i/image.png',__FILE__),
                 'menu_position' => null,
                 'supports' => array( 'title', 'author','comments')
             );
     #} Register it
     register_post_type('picsmash',$args);

 }}}

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


More information about the wp-trac mailing list