WordPress Code To Improve Bulk Insert Or Update Post Speed
When you hit tens of thousands of posts in WordPress, the speed of wp_insert_post or wp_update_post gets significantly slower. To improve the speed, you simply add the following code and you will see that the speed improves significantly.
Add these two lines of code before your insert or update loop
Noticed that google isn’t really useful in this case (there’s no copy and paste answer). Google for me only shows a bunch of people having issues and no easy solution when in fact the solution is quite simple
First things first, you have to check to see if your PHP-PhantomJs library version is 3.x or 4.x
If it is 3.x, to change the user-agent string, you just have to do this
$request->addSetting('userAgent', 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0');
If it is 4.x, to change the user-agent string, you just have to do this
$request->addHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0');
Of course, feel free to change the new value of your user-agent to whatever you want it to be.
To verify that it is working, if your testing locally, then you can use tools such as Fiddler to check the headers. Alternatively, you can dump out the $request and $response variables, like so,
var_dump($request); // $request is the return value of either createRequest or createCaptureRequest function
var_dump($response); // $response is the return value of createResponse function
If you have any questions feel free to let me know in the comments below.
Enjoyed the content ? Share it with your friends !