Custom Taxonomy not showing in Post Gutenberg editor
Asked Answered
I

4

13

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 );
}
Izzy answered 26/4, 2019 at 15:45 Comment(0)
S
35

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.

Sara answered 2/5, 2020 at 18:25 Comment(0)
C
12

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
);
Cateran answered 30/4, 2019 at 17:30 Comment(2)
Hey @artem, thanks for this, but as you can see from my question and code, this is already in place?Izzy
Thank you for this solution, wasn't aware of 'show_in_rest' => true,Mcglone
I
2

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.

Izzy answered 1/5, 2019 at 13:18 Comment(1)
'show_in_rest' => true parameter added in both argument of register_post_type and register_taxonomy and register_taxonomy slug is not as reserved terms of wordpress. But the taxonomy is not visible on the post edit. Category metabox display only default category on it. Can you please help me ?Scheme
C
0

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.

Cushitic answered 28/3, 2021 at 21:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.