Afaf
Afaf

Reputation: 654

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);

Upvotes: 0

Views: 1149

Answers (2)

devLad
devLad

Reputation: 16

Try please wp_set_post_terms or wp_set_object_terms

Upvotes: 0

Afaf
Afaf

Reputation: 654

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' );

Upvotes: 3

Related Questions