I developed a plugin to read some XML files and publish their itens as post but for some reason, sometimes, it is duplicating posts. Basically that is what I have:

// function one is scheduled (hourly) and unscheduled on plugin activation and deactivation hooks
register_activation_hook(__FILE__, 'function_one_activation');
register_deactivation_hook(__FILE__, 'function_one_deactivate');

function function_one_activation() {
    if (!wp_next_scheduled('function_one_cron')) {
        wp_schedule_event(time(), 'hourly', 'function_one_cron');

function function_one_deactivate() {
    $timestamp = wp_next_scheduled('function_one_cron');
    wp_unschedule_event($timestamp, 'function_one_cron');

add_action('function_one_cron', 'one');
add_action('function_two_cron', 'two');
add_action('function_three_cron', 'three');

function one() {
    // download XML files using CURL and save on server
    // schedule cron to run function two after 60 seconds
    if(!wp_next_scheduled('function_two_cron')) {
        wp_schedule_single_event(time() + 60, 'function_two_cron');

function two() {
    // get an array from DB of posts saved from XML files
    // get and run each file and check if item is new (is not on array above) and save it as post, if already posted remove it ID from array
    // save the "new" array
    // schedule cron to run function three after 60 seconds
    if(!wp_next_scheduled('function_three_cron')) {
        wp_schedule_single_event(time() + 60, 'function_three_cron');

function three() {
    // get the remain ID from array above and delete posts

Does anyone know what I am missing that makes it runs twice sometimes or how can I prevent it to happens?

  • 1
    note that your functions refer to accessing arrays from other functions, but that's not how requests in PHP work. When a request has been handled, everything is wiped clean, and the next request loads WP again from a blank slate. Variables do not persist, and there is no constant application process like a Node or Java web application. It's also unclear why you're scheduling single cron jobs from a plugin activation hook, this is highly unusual. Are you using a load balancer or multiple servers?
    – Tom J Nowell
    Jun 1, 2022 at 20:45
  • Hey thank you for replying... I hidden some part of code just to ilustrate the step by step process, even the functions name I changed here and I didn't mentioned this array is saved as an option_value using wp_options table to persist the data... the plugin is working fine, but sometimes it causing the wp-cron.php to process more than once Jun 2, 2022 at 9:56
  • Are you using a load balancer or multiple servers/computers to serve your site? Have you checked with a plugin such as WP Crontrol or the WP CLI cron commands for duplicates scheduled entries? It's critically important that you answer the load balancing/multiple servers question as quickly as possible.
    – Tom J Nowell
    Jun 3, 2022 at 2:57
  • As far as I know I am not using load balancer but I don't know much about hardware. WP Control only shows one cron for the first function since the other two are being scheduled as a single event and not hourly as the first one I think what might be happen is that the whole process is taking too long to finish and the second function is still running after one hour and function one starts running again, how can check if a single cron event is still running? Jun 3, 2022 at 17:44
  • I would strongly advise against having cron jobs triggering other cron jobs, it's much more efficient and reliable to have a regular cron job that checks for tasks in a queue to do, rather than individual cron jobs that are chained together. If you had a load balancer or multiple servers then there would be a trivial fix of only running WP Cron on a single server
    – Tom J Nowell
    Jun 5, 2022 at 9:34


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.