wp_insert_post extremely slow on big table, direct query very fast

you should revers the code you should add this befor the import

wp_defer_term_counting( true );
wp_defer_comment_counting( true );

don’t set it false and after the import you set it true you must do the oppsite thing
i had the same issue to insert 50 posts it took about 7 mintues after i added this code befor the wp_insert_post it took just 7 seconds
and you can add this too

define( 'WP_IMPORTING', true );

and after you imported the post add this and run the script

wp_defer_term_counting( false );
wp_defer_comment_counting( false );

Leave a Comment