Cannot choose custom categories for custom post type in post editor

admin2025-06-02  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.

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.

Share Improve this question asked Mar 3, 2019 at 12:18 Ole Kristian LosvikOle Kristian Losvik 1572 silver badges9 bronze badges 4
  • Replace 'hierarchical' => false with 'hierarchical' => true – Chinmoy Kumar Paul Commented Mar 3, 2019 at 13:40
  • Thanks for the suggestion @ChinmoyKumarPaul, however categories does still not appear. – Ole Kristian Losvik Commented Mar 3, 2019 at 13:42
  • Why are you set the priority 0? Change it to 10 or 11 and test once? – Chinmoy Kumar Paul Commented Mar 3, 2019 at 14:01
  • Thanks for the suggestion @ChinmoyKumarPaul, I tried changing priority to 10 and 11 but still does not appear. – Ole Kristian Losvik Commented Mar 3, 2019 at 14:06
Add a comment  | 

1 Answer 1

Reset to default 3

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748845917a314209.html

最新回复(0)