I’m running a cron every 5:th minute that inserts a lot of posts. Sometimes it happens that those crons are running both at the same time. To be sure that not causing duplicate posts. I would like to use sql lock tables during an insert. But that is problematic with wp_insert_post() since that is doing stored sql processes.

Would not like to use regular SQL’s since I don’t get the benefits of everything wp_insert_post() does. And could be affected by future wp updates as well.

Is there any way to use wp_insert_post() WITH locked tables?

1 Answer
1

Firstly, your question indicates that your plugin sometimes runs over 5 min, don’t you get script timeouts? 5 min is a very long runtime… How many posts are you adding and what’s your server environment?

Back to the problem…

May I suggest another solution?

Simply set an option that you’re doing something, and deactivate it when you’re done.

Example

function your_function() {

  $is_already_running = get_option('is_my_insert_post_running');

  if( ! $is_already_running) {
    update_option('is_my_insert_post_running', 1); // set is running trigger

    // do your post inserting here

    // When you're done, don't forget to deactivate
    update_option('is_my_insert_post_running', 0);

  } else {
    // wait for the next cron, i'm still working on the last one
  }
}

Regards, Bjorn

Leave a Reply

Your email address will not be published. Required fields are marked *