recursive wp_remote_post() | WordPress.org

I am trying to create recursive function with wp_remote_post() and wp_ajax_nopriv_ action. My code:

add_action('wp_ajax_recursive_func', 'recursive_func');
add_action('wp_ajax_nopriv_recursive_func', 'recursive_func');
function recursive_func(){
   $offset = (!empty($_POST['offset'])) ? $_POST['offset'] : 0;

   $posts_args = array(
        'post_type' => 'post',
        'nopaging' => false,
        'numberposts' => 50, 
        'offset' => $offset,
    );
    $posts = get_posts($posts_args);

   if ($posts){
      // do something
      wp_remote_post(admin_url('admin-ajax.php?action=recursive_func'), [
        'method' => 'POST',
        'blocking' => false,
        'sslverify' => false,
        'body' => [
            'offset' => $offset
        ]
    ]);

     $offset = $offset + 50;
   } else {
      return false;
   }
}

when i debug i get the following
1) the function recursive_func runs for first time
2) the $offset is 0
3) the get_posts() returns 50 posts
4) the wp_remote_post() runs
5) the function recursive_func runs for second time
6) the $offset is 50
7) the get_posts() returns 50 posts
7) the wp_remote_post() does not run

any idea?

  • This topic was modified 2 days, 13 hours ago by The Three.



Source link