'Wp_insert_post does not add post category

I can create a new category using wp_insert_category but I can not add it to my post, any suggestion please!

$cat = array( 
                  'cat_name' => 'dossiers-a-suivre',
                  'cat_slug' => 'dossiers-a-suivre',
                  'taxonomy' => 'category' );

        $cat_id = wp_insert_category( $cat );

        $my_post = array(
                'post_title' => "post test",
                'post_content' => 'This is my post.',
                'post_date' => date('Y-m-d H:i:s'),
                'post_type' => 'folder',
                'post_category' => array( $cat_id)
            ); 


        $post_id = $this->insert_post($my_post);


Solution 1:[1]

I solved the problem by using wp_set_object_terms :)

$cat = array( 
                  'cat_name' => 'dossiers-a-suivre',
                  'cat_slug' => 'dossiers-a-suivre',
                  'taxonomy' => 'category' );

        $cat_id = wp_insert_category( $cat );

        $my_post = array(
                'post_title' => "post test",
                'post_content' => 'This is my post.',
                'post_date' => date('Y-m-d H:i:s'),
                'post_type' => 'folder',
                'category_name' => 'dossiers-a-suivre',
            ); 


        $post_id = $this->insert_post($my_post);

        wp_set_object_terms($post_id, $cat_id, 'category' );

Solution 2:[2]

Try please wp_set_post_terms or wp_set_object_terms

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Afaf
Solution 2 devLad