wp_insert_post Consuming Large Amounts of Memory

0
1381

wordpress_earl-150If you are using “wp_insert_post” to insert large amounts of posts from external scripts or even code within WordPress and notice “wp_insert_post memory consumption” is very high, “increasing PHP memory” may not be the solution you are looking for.

The reason for this is simply the fact that Wordpres Caches its queries and you need to prevent this from happening for the duration of execution of your code.

To accomplish this make the following two changes.

Add this line to your wp-config.php file

define( 'SAVEQUERIES', false );

This prevents WordPress from caching all the SQL Query’s and run-time results for the session.

Next add the following line in your script file, the location can be before the “wp_insert_post loop” or after you have defined WordPress.

wp_suspend_cache_addition(true);

This will prevent the result of each wp_insert_post from getting into the “Worpdress Object Cache” which is generally what consumes the memory.

Now you can insert millions of posts into WordPress without ever having to worry about memory issues.

For an internal scripts make sure you turn back wp_suspend_cache_addition(true); on once your inserts have been completed.

LEAVE A REPLY

Please enter your comment!
Please enter your name here