Assigning a category to a custom post type in WordPress

Couple things I’ve spotted:

  1. Your taxonomy registration register_taxonomy( 'document-category', 'Documents' should be register_taxonomy( 'document-category', 'document' – the second parameter is the post type name(s), not the post type label.

  2. If you’re programmatically inserting posts then forget using tax_input in your wp_insert_post() data args. There’s a hard-coded permissions check on the current user and there’s nothing you can do to override it:

    // New-style support for all custom taxonomies.
    if ( ! empty( $postarr['tax_input'] ) ) {
        foreach ( $postarr['tax_input'] as $taxonomy => $tags ) {
            $taxonomy_obj = get_taxonomy( $taxonomy );

            if ( ! $taxonomy_obj ) {
                /* translators: %s: Taxonomy name. */
                _doing_it_wrong( __FUNCTION__, sprintf( __( 'Invalid taxonomy: %s.' ), $taxonomy ), '4.4.0' );
                continue;
            }

            // array = hierarchical, string = non-hierarchical.
            if ( is_array( $tags ) ) {
                $tags = array_filter( $tags );
            }

            if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
                wp_set_post_terms( $post_ID, $tags, $taxonomy );
            }
        }
    }

Instead take note from the code above and do it yourself:

$post_id = wp_insert_post( $user_post );

wp_set_post_terms( $post_id, [ 4 ], 'document-category' );