Search code examples
phpwordpresscustom-post-typetaxonomycustom-taxonomy

Custom taxonomy box displays standard categories...(WordPress)


Ok so, my problem is simple. I'm working in WordPress. So I've created Custom Post Type (aka CPT), and then a Custom Taxonomy (CT) in order to organize my CPT. I'm adding the code to register a new taxonomy and to link it with my CPT. But when i'm going into the CPT editing page, the CT box displays the standard categories (which are "Actualités", "Agenda","Non classé" and "Publications") instead of the custom ones (which are "Enfance", "Sport" and "Informatique") that i've just created before.

Below the concerned code :

/**
* Permet de créer un type de posts personnalisé, avec un menu dédié
* concernant les associations, leur ajout, édition, suppression, etc...
*/
function wp_custom_post_type() {
    $labels = array(
        'name'                => _x( 'Associations', 'Post Type General Name'),
        'singular_name'       => _x( 'Association', 'Post Type Singular Name'),
        'menu_name'           => __( 'Associations'),
        'all_items'           => __( 'Toutes les associations'),
        'view_item'           => __( 'Voir l\'association'),
        'add_new_item'        => __( 'Ajouter une nouvelle association'),
        'add_new'             => __( 'Ajouter'),
        'edit_item'           => __( 'Editer l\'association'),
        'update_item'         => __( 'Modifier l\'association'),
        'search_items'        => __( 'Rechercher une association'),
        'not_found'           => __( 'Non trouvée'),
        'not_found_in_trash'  => __( 'Non trouvée dans la corbeille'),
    );

    $args = array(
        'label'               => __( 'Associations'),
        'description'         => __( 'Annuaire des associations des Coteaux Bordelais.'),
        'labels'              => $labels,
        'supports'            => array( 'title', 'thumbnail'),
        'menu_icon'           => 'dashicons-admin-home',
        'hierarchical'        => false,
        'public'              => true,
        'has_archive'         => false,
        'rewrite'             => array( 'slug' => 'associations'),
    );
    register_post_type( 'associations', $args );
}

/**
 * Crée des catégories spécifiques pour les associations 
 * afin de ne pas les mélanger avec les catégories normales. 
 */
function wp_add_taxonomies() {
    $labels_asso_taxonomy = array(
        'name'                       => _x('Catégories d\'association', 'taxonomy general name'),
        'singular_name'              => _x('Catégorie d\'association', 'taxonomy singular name'),
        'search_items'               => __('Rechercher une catégorie'),
        'popular_items'              => __('Catégories populaires'),
        'all_items'                  => __('Toutes'),
        'edit_item'                  => __('Éditer une catégorie'),
        'update_item'                => __('Mettre à jour une catégorie'),
        'add_new_item'               => __('Ajouter une nouvelle catégorie'),
        'new_item_name'              => __('Nom de la nouvelle catégorie'),
        'add_or_remove_items '       => __('Ajouter ou supprimer une catégorie'),
        'choose_from_most_used'      => __('Choisir parmi les catégories les plus utilisées'),
        'not_found'                  => __('Aucune catégorie d\'association trouvée'),
        'menu_name'                  => __('Catégories d\'association'),
    );
        $args_cat_association = array(
        'hierarchical'      => true,
        'labels'            => $labels_asso_taxonomy,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array(
            'slug' => 'les-associations',
            'with_front' => true 
        )
    );
    register_taxonomy('les_associations', 'associations', $args_cat_association);
    register_taxonomy_for_object_type('les_associations', 'associations');
}

What did I do wrong ?

Here the render of the problem situation : [The problem] --> https://necronhdx.fr/images/Screenshots/chrome_2018-01-10_15-51-27.png

Thank you for your time and, I hope, your answers


Solution

  • If you remove this line : register_taxonomy_for_object_type('les_associations', 'associations');

    and where you have the $args for the custom post type you add:

    'taxonomies'=> array( 'les_associations' ),

    That should do the trick.