1

I have written a bulk import script where I import posts with the wp_insert_post() function and add 1 meta value and 1 term to the post.

I have done 6 times of 7000 posts now, but for some reason the more posts are in my database the fewer posts are being imported at a time.

Can anyone explain why this happens, because in my opinion each new wordpress post is the same action and is independent from the rest. The only thing which is getting bigger is the post ID, and postmeta post_id.

I shortened the script, but this is the full version: http://codepad.org/0K7cHQNI

This is my script:

if( ! $errors ) {

    foreach( $lines as $line ) {

        // Import post
        $args = array(
            'post_title'    => $new_vipost_title,
            'post_content'  => $new_vipost_content,
            'post_status'   => 'publish',
            'post_type'     => $vipost_type,
        );
        $post_id = wp_insert_post( $args );

        update_post_meta( $post_id, 'vimport_key', $import_key );   

        // Set term if isset
        wp_set_object_terms( $post_id, $viterm, $vitax, false );
    }       

} else {
    $_POST['errors'] = true;    
}
  • 1
    First: Please don't make a question relying on outside code. If the link is gone, no one will understand your question. File an edit and put it all in your question. When I look at the linked code, then there's much more going on. The amount of queries is quite high. The question is if there's a possibility to reduce them. You might want add $GLOBALS['wp_query']->last_query after each of the queries and add the output of the queries to your question. That would make it easier. – kaiser Sep 15 '14 at 13:04
  • 1
    Maybe this is helpful to you? – birgire Sep 15 '14 at 13:53
2

I found the solution by the comment from birgire what tells to use wp_defer_term_counting() as follows:

    if( ! $errors ) {

        // Set wp_defer_comment_counting();
        wp_defer_term_counting( true );

        foreach( $lines as $line ) {

            // Import post
            $args = array(
                'post_title'    => $new_vipost_title,
                'post_content'  => $new_vipost_content,
                'post_status'   => 'publish',
                'post_type'     => $vipost_type,
            );
            $post_id = wp_insert_post( $args );

            update_post_meta( $post_id, 'vimport_key', $import_key );   

            // Set term if isset
            wp_set_object_terms( $post_id, $viterm, $vitax, false );
        } 

        // Set wp_defer_comment_counting( false );
        wp_defer_comment_counting( false );      

    } else {
        $_POST['errors'] = true;    
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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