0

I am trying to make a custom post type for short texts, notici. I want to register a custom taxonomy for Notice categories. I have tried the code below in my plugin. The tags does show up and works, however I cannot see or choose categories when editing the notici post.

<?php


class Notici_Register_Cpt {
    function __construct( $plugin_name, $version ) {

        // Register the custom post type
        add_action( 'init', array( $this, 'notici_register_cpt' ) );

        // Add notici categories
        add_action( 'init', array( $this, 'notici_register_category_taxonomy' ), 0 );

    }

    function notici_register_cpt() {

        $labels = array(
            'name'               => _x( 'Notices', 'post type general name' ),
            // omitted for brevity
        );

        $args = array(
            'label'             => __( 'Notices' ),
            'labels'            => $labels,
            'public'            => true,
            'show_in_rest'      => true,
            'has_archive'       => true,
            'can_export'        => true,
            'show_ui'           => true,
            '_builtin'          => false,
            'capability_type'   => 'post',
            'menu_icon'         => 'dashicons-exerpt-view',
            'hierarchical'      => false,
            'rewrite'           => array( 'slug' => get_option( 'notici_slug' ) ),
            'supports'          => array( 'title', 'thumbnail', 'excerpt', 'editor' ),
            'show_in_nav_menus' => true,
            'taxonomies'        => array( 'noticicategory', 'post_tag' ),
        );

        register_post_type( 'notici', $args );
        flush_rewrite_rules();

    }

    // Register Custom Taxonomy
    function notici_register_category_taxonomy() {

        $labels = array(
            'name'                       => _x( 'Notice categories', 'Taxonomy General Name', 'notici' ),
            // omitted for brevity
        );
        $args   = array(
            'labels'            => $labels,
            'hierarchical'      => false,
            'public'            => true,
            'show_ui'           => true,
            'show_admin_column' => true,
            'show_in_nav_menus' => true,
            'show_tagcloud'     => true,
            'rewrite'           => array( 'slug' => 'notice-category' ),
        );
        register_taxonomy( 'noticicategory', array( 'notici' ), $args );
    }
}

Some code skipped for brevity, here is everything.

  • Replace 'hierarchical' => false with 'hierarchical' => true – Chinmoy Kumar Paul Mar 3 at 13:40
  • Thanks for the suggestion @ChinmoyKumarPaul, however categories does still not appear. – Ole Kristian Losvik Mar 3 at 13:42
  • Why are you set the priority 0? Change it to 10 or 11 and test once? – Chinmoy Kumar Paul Mar 3 at 14:01
  • Thanks for the suggestion @ChinmoyKumarPaul, I tried changing priority to 10 and 11 but still does not appear. – Ole Kristian Losvik Mar 3 at 14:06
1

You have to make your taxonomy visible in rest - otherwise it won’t be visible in Block Editor (Gutenberg).

There is show_in_rest arg for register_taxonomy. It’s default value is false, so you have to set it to true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.