function workQueue( $jobs = 10, $attemptsPerJob = 3 ){ //Get a properly configured queue $queue = wp_queue(); //Get the worker for the queue $worker = $queue->worker($attemptsPerJob); //Loop through jobs in queue until we've done the number passed in $jobs // or no jobs left to run $totalJobs = 0; while( $worker->process() && $jobs <= $totalJobs ){ $totalJobs++; } //Work the queue and return rest_ensure_response( [ 'totalJobs' => $totalJobs ] ); } add_action( 'rest_api_init', function(){ register_rest_route( 'hi-roy', 'queue/run', [ 'methods' => ['POST', 'GET' ], 'callback' => 'workQueue' ]); });