Insert custom post type with specific categories

I am tryig to insert my custum post type with a specific taxonomies , but it doesn’t work

Here my Custum post type

Read More
add_action( 'init', 'create_annonce' );
function create_annonce()
{
register_post_type( 'wp_annonce',
        array(
            'labels' => array(
                'name' => __( 'Annonces' ),
                'singular_name' => __( 'Annonce' ),
                'add_new' => __( 'Ajouter une annonce' ),
                'all_items' => __( 'Tous les annonces' ),
                'add_new_item' => __( 'Ajouter une annonce' ),
                'edit_item' => __( 'Modifier annonce' ),
                'new_item' => __( 'Nouvelle annonce' ),
                'view_item' => __( 'Voir annonce' ),
                'search_items' => __( 'Recherche annonces' ),
                'not_found' => __( 'makayench :D' ),
                'not_found_in_trash' => __( 'No announce found in trash' )
                //'menu_name' => default to 'name'
            ));
// my taxonomy 
    register_taxonomy( 'annonce_category', 'wp_annonce',
            array( 'hierarchical' => true,
                'label' => __( 'Categories des annonces' )
            )
        );
    }

I created all categories i need (‘a’,’b’,’c’) and I gave the user a list of checkbox to check a category

if (isset($_POST['category']) && is_array($_POST['category'])) {
    foreach($_POST['category'] as $category) 
        {echo $category;
    $tableau=array($_POST['category']);}
}

The array is nt empty , now I try to insert the post with my categories

// $post is declared in my code 
$the_post_id=wp_insert_post($post);
wp_set_post_terms($the_post_id,$tableau,'annonce_category',true);

Now , the Costum post type is created but without a categories , Any help

Related posts

Leave a Reply