Custom function counts posts but only up to 10

I'm trying to find a way to count publish posts and then saves them to a custom field and updates the permalink. My permalink structure is saved as '/episode/$number'. I think the best way for this is to use 'wp_insert_post'.

The code below works until the 10th post; after that, every post gets counted as 10, 10 is then saved to the custom field and all permalinks get saved as 10-2.

add_action( 'init', function() {
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure( '/episode/%postname%/' );
});
function updateNumbers( $data, $postarr ) {
    if ( !empty( $postarr['ID'] ) && ! wp_is_post_revision( $postarr['ID'] ) ) {
        if ( !( $counts = get_post_meta( $postarr['ID'], 'incr_number', true ) ) ) {
            global $wpdb;
            $counts = (int)$wpdb->get_var( "SELECT MAX(meta_value) FROM {$wpdb->postmeta} WHERE meta_key='incr_number'" );
            $counts++;
            update_post_meta( $postarr['ID'], 'incr_number', $counts );
        }
        $data['post_name'] = $counts;
    }
    return $data;
    }
add_filter('wp_insert_post_data', 'updateNumbers', 10, 2 );