Wp_insert_post不添加帖子类别

时间:2017-02-23 12:30:07

标签: wordpress

我可以使用wp_insert_category创建一个新类别,但我无法将其添加到我的帖子中,请提出任何建议!

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

2 个答案:

答案 0 :(得分:2)

我使用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' );

答案 1 :(得分:0)

请尝试wp_set_post_terms或wp_set_object_terms