$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Cannot choose custom categories for custom post type in post editor|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Cannot choose custom categories for custom post type in post editor

matteradmin9PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far