Issues When Recursively Calling wp_schedule_single_event()

As I can understand from your code, you need to run the code only once to get the data, but if it failed, so run again once, untill you get the data then stop. according to this understanding we can think like the following:

  • We need to depend on wp_schedule_event to make sure the event will continue firing, untill it meets a condition (will be defined). so use it instead of wp_schedule_single_event

  • Create a flag that will be set to a value if the function returned the desired data..

  • This flag should be the condition the controls the code running.

  • If the condition is not met so we run the code, else we remove the hooked event

So we can write the code:

<?php
add_action('order_drip_followers_hook','order_drip_followers', 10, 2);
function order_drip_followers($workflow, $days)
{
if(!get_transient('user_meta_updated_'.get_current_user_id( )){


$link = 'https://www.instagram.com/';
$order = $workflow->data_layer()->get_order();
$ig_user = clean($order->get_meta('ig_username'));
$forward_slash="https://wordpress.stackexchange.com/";
$profile_link = $link . $ig_user . $forward_slash;
$customer = $workflow->data_layer()->get_customer();
$customer_user_id = $customer->get_user_id();

    $postData = array(
    //removed for privacy
    );

    $request = curl_post_followers($postData);
    var_dump($request);

    $payload = json_decode($request,true);
    $order_status = $payload["status"];

    if($order_status == "ok") {
        //API Order Success
        $order_id = $payload["order"];
        update_user_meta($customer_user_id, "drip_followers_order_id", $order_id);
        set_transient('user_meta_updated_'.get_current_user_id( ), '1' );
   }
   }else{
    wp_clear_scheduled_hook( 'order_drip_followers_hook', array( $workflow, $day ) );
   }
}
 ?>

I didn’t test the code, I hope you test and inform me what happens.

Note

If you just need the code run always for specific intervals so no need for the flag.