dungey_140
dungey_140

Reputation: 2802

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 );
}

Upvotes: 13

Views: 12548

Answers (4)

Amro K Saleh
Amro K Saleh

Reputation: 21

The problem you are facing could be solved by changing your 'rewrite' attribute in the list from true into array( 'slug' => 'your-taxonomy-slug' ) so the new 'rewrite' should look like 'rewrite' => array( 'slug' => 'your-taxonomy-slug' ) and you can put any slug instead of your-taxonomy-slug.

The reason why this should work I don't fully understand, but it looks like the block editor uses the Rest API that for some reason can't deal with the taxonomy without rewriting the slug! something is weird there because all the other features work fine it's just inside the Gutenberg only for some reason.

If this didn't work it could be a conflicting theme or plugin... try deactivating stuff to see what causes the conflict.

Upvotes: 0

Deepak Rajpal
Deepak Rajpal

Reputation: 1011

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.

Upvotes: 36

dungey_140
dungey_140

Reputation: 2802

It seems that after much investigation, the issue was not with the code above (which is correct). The issue was with a second custom taxonomy, named 'type'. As it turns out, Wordpress contains a number of 'reserved terms', of which one is 'type'. Once this taxonomy was renamed, both taxonomies work correctly, including with Gutenberg.

Upvotes: 2

Artem
Artem

Reputation: 765

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
);

Upvotes: 12

Related Questions