Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Taxonomy not showing in Post Gutenberg editor

I have registered a custom taxonomy in Wordpress, and I can't work out why it is not displaying in standard Wordpress posts, since Gutenberg has been introduced. What I mean by this, is that it does not display in the document sidebar when adding or editing a post. The same is true for 'Categories' and 'Tags', which are obviously standard taxonomies.

I have ensured 'show_in_rest' => true is present is the taxonomy registration, but it hasn't made any difference.

It seems that they are partially registering, as they show up under 'Posts' in the main left hand menu, which suggests it might be Gutenberg related?

Any ideas?

// Register taxonomy
add_action( 'init', 'register_taxonomy_articles_element' );

function register_taxonomy_articles_element() {

    $labels = array( 
        'name' => _x( 'Elements', 'articles_element' ),
        'singular_name' => _x( 'Element', 'articles_element' ),
        'search_items' => _x( 'Search Elements', 'articles_element' ),
        'popular_items' => _x( 'Popular Elements', 'articles_element' ),
        'all_items' => _x( 'All Elements', 'articles_element' ),
        'parent_item' => _x( 'Parent Element', 'articles_element' ),
        'parent_item_colon' => _x( 'Parent Element:', 'articles_element' ),
        'edit_item' => _x( 'Edit Element', 'articles_element' ),
        'update_item' => _x( 'Update Element', 'articles_element' ),
        'add_new_item' => _x( 'Add New Element', 'articles_element' ),
        'not_found' => _x( 'No Elements found', 'articles_element' ),
        'new_item_element' => _x( 'New Element', 'articles_element' ),
        'separate_items_with_commas' => _x( 'Separate Elements with commas', 'articles_element' ),
        'add_or_remove_items' => _x( 'Add or remove elements', 'articles_element' ),
        'choose_from_most_used' => _x( 'Choose from the most used elements', 'articles_element' ),
        'menu_name' => _x( 'Elements', 'articles_element' )
    );

    $args = array( 
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_in_nav_menus' => true,
        'show_in_rest' => true,
        'show_ui' => true,
        'show_tagcloud' => true,
        'hierarchical' => true,
        'rewrite' => true,
        'query_var' => true
    );

    register_taxonomy( 'element', array('post'), $args );
}
like image 873
dungey_140 Avatar asked Apr 26 '19 15:04

dungey_140


2 Answers

If someone still having an issue displaying taxonomy or Gutenberg editor, add 'show_in_rest' => true, in both custom post types as well as taxonomy arguments.

like image 164
Deepak Rajpal Avatar answered Oct 02 '22 17:10

Deepak Rajpal


Since Gutenberg is working based on REST API you need to turn on support for REST API for any custom post type and taxonomy. You need to add additional key show_in_rest = true to your $args array. Your full code should look like this:

$args = array( 
    'labels' => $labels,
    'public' => true,
    'show_in_rest' => true, // add support for Gutenberg editor
    'publicly_queryable' => true,
    'show_in_nav_menus' => true,
    'show_in_rest' => true,
    'show_ui' => true,
    'show_tagcloud' => true,
    'hierarchical' => true,
    'rewrite' => true,
    'query_var' => true
);
like image 30
Artem Avatar answered Oct 02 '22 15:10

Artem