prefix . 'xyz_ihs_usage'; // Get posts starting from $offset $posts = $wpdb->get_results($wpdb->prepare( "SELECT ID, post_content, post_type FROM {$wpdb->posts} WHERE post_status = 'publish' ORDER BY ID ASC LIMIT %d, %d", $offset, $batch_size )); if (empty($posts)) { // Finished update_option('xyz_ihs_sync_needed', 0); // reset wp_send_json_success(['status' => 'complete']); } foreach ($posts as $post) { // Run your usage update logic xyz_ihs_update_usage_for_post($post->ID, $post->post_content, $post->post_type); } $new_offset = $offset + count($posts); // Store the new offset update_option('xyz_ihs_sync_needed', $new_offset); wp_send_json_success([ 'status' => 'processing', 'new_offset' => $new_offset ]); }); ?>