• Whenever I have to run code on the database, change posts, or terms or what have you, I am running it on a custom page template.

    Since this has been working for me up to know, I didn’t think about it much. But I need to delete a ton of terms now from a custom taxonomy and I can’t do it on the test page very effectively. Meaning I get 504 gateway errors all the time, because the code takes too long to run, and deletes only a part of the terms.

    So I am wondering, if I need to run custom code to change a lot of data, what is the most efficient method to use?

    • This topic was modified 4 years, 4 months ago by lfbender.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    It depends upon the scope of the task you need to accomplish. You might be able to simply increase the time allocated to execute scripts. Add something like set_time_limit ( 120 ); to wp-config.php. The default is usually 30 sec. I’d advise not setting it for too long though, if a coding issue is encountered it can be difficult to regain control with very long time limits. You should comment out the line (or set it to 30) when your need for extra time is over.

    I also like using page template code to accomplish special tasks. Things that take a long time usually involve querying for data to operate upon. When a lot of data is involved I set a limit on the query so my code only operates on the first X number of records found. I then just keep reloading the page to repeatedly run my script on another chunk of data until no more records are found to work upon. The value of X varies depending on the task being done. A few dozen to a few hundred records at a time is usually adequate.

Viewing 1 replies (of 1 total)
  • The topic ‘How to run code in WordPress’ is closed to new replies.