We came across a situation where we had to use the wp_update_post() WordPress function to insert custom posts from another database. This function is the best to use since it create not only the posts, but also generate the "post_name". The basic code for calling this function is the following:
$post_id = wp_insert_post(array('post_author'=>1, 'post_title'=>$post_title, 'post_type'=>$post_type, 'post_status'=>'publish'));
Basically when the post_status is set to "publish", this function do a lot of other things that make it super slow when you have a lot of posts in your "wp_posts" tables. The more posts you get, the slower this function seems to be.
We had about 20 000 rows which is not really a lot for the mysql database, but with this function trying to import 10 000 other rows it gets really slow and can take more than 30 min just to import 10 000 new entries ! There is 2 solutions. Solution one is to set the post_status to "draft". Your execution time will be much much faster. It will tale you some minutes at most. But your new posts will not have a post_name generated (used for the permalinks) and you would need to have another process to set the posts statuses to "publish". Not ideal. Solution two is basically to declare these 2 function before your loop that is calling the wp_update_post() function:
And to call the same functions with the opposite boolean value at the end of the loop, like this:
And no there is no mistake the "wp_defer_term_counting" is set to false to keep the term counting to false. The function name is actually misleading. Using these functions has speed up our import by at least 10 times ! If you have a better solution please let us know!