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 developing a bulk importer. It basically takes images and converts them into posts. Each image will be created with 10 different related posts therefore each image will represent/create 11 posts. If I import 100 images, the plugin would have to create 1100 posts successively. The problem is Wordpress throws a 404 error in the console while the plugin is creating the posts. Here's the exact error:

Failed to load resource: the server responded with a status of 404 (Not Found) http://www.example.com/wp-admin/admin-ajax.php

The error log says:

WordPress database error MySQL server has gone away for query ...

The plugin does the import by uploading the images first and then create the posts after that. It's in the creation of the posts when the error is thrown.

It's important to note though that the error does not occur if I import under 20 images.

As this is AJAX, I don't have other error data that would help me diagnose the problem. But I'm guessing the server somehow stops at a certain point while being asked to create posts. Any thoughts?

EDIT: As requested by @kaiser, here's the function the creates the post for each image:

public function create( $posted ) {
    $post_ids = array();

    foreach ( $posted['attachments'] as $file_id ) {
        $file_data = $this->get_file( $file_id );

        $post = array(
          'post_title' => $posted['titles'][$file_id] ? $posted['titles'][$file_id] : $file_data['name'],
          'post_content' => '',
          'post_type' => 'xxx',
          'post_status' => 'publish',
          'post_author' => get_current_user_id(),
          'tax_input' => array( 
                                'xxx_type' => array( post_type_term_id() ), 
                                'xxx_cat' => array( $posted['term_id'] )
                              )
        );

        // Create post
        $post_id = wp_insert_post( $post );
        $post_ids[] = $post_id;

        // Form post metadata
        $metadata = xxx_metadata_defaults();

        // Attach event date to this post
        $metadata[ 'event_date_meta_key' ] = date( 'Y-m-d H:i:s', strtotime( $posted['date'] ) );

        // Add meta that determines if this post is imported by this plugin
        $metadata['xxx'] = 'yes';
        // Record attachment id for later use
        $metadata['xxx_id'] = $file_id;

        // Update metadata
        foreach ( $metadata as $key => $value ) {
          update_post_meta( $post_id, $key, $value );
        }

        // Set file as the post thumbnail for the post
        set_post_thumbnail( $post_id, $file_id );

        // Create related
        $this->create_related( $posted['group'], $post_id, $file_data );

        do_action( 'create_related_posts_complete', $post_id, $posted['term_id'], $posted['users'] );
    }

    return $post_ids;
}

The line below creates another 10 posts.

// Create related
$this->create_related( $posted['group'], $post_id, $file_data );

I'm not sure how to split the query since all the files are fetched from the form and created as posts at once so maybe I have to use sleep() as @kaiser suggested. But how much time should I assign to sleep? Will it depend on some php.ini config -- max_execution_time?

share|improve this question
2  
enable logging of errors to a file –  Milo Sep 20 '13 at 1:21
    
@Milo: I just included the error in the log file. Thanks! –  Rade Sep 20 '13 at 1:24
1  
MySQL server has gone away is probably a timeout error. Not sure how your import works, but you should break it up into multiple consecutive requests if you're not already. –  Milo Sep 20 '13 at 1:31
    
Please show us your code - the relevant parts. –  kaiser Sep 20 '13 at 10:59
    
WP has no issue creating large amounts of posts, but it takes time and resource toll. You are probably knocking out MySQL server, which is separate beast entirely from WP. –  Rarst Sep 20 '13 at 14:08

1 Answer 1

What I did for custom importers:

  • Add a dedicated menu page in admin
  • Step-process it and echo in between
  • Use sleep() after each import
share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.