Making Custom Post Type & Taxonomy Works With Gutenberg

Gutenberg – the all new WordPress admin editor was released since WordPress 5.0. It brought a whole new way on how to create post & page within WordPress, it’s like using using a block-based page builder inside WordPress Dashboard.

There are many pros and cons amongst the WordPress community since it was first introduced but like it or not we just have to accept that Gutenberg is here and looks like it will stay for a long time.

If you’re a developer, you might want to adjust your theme or plugin if they have their own custom post type and taxonomy. By default, if you create a custom post type it will still use the classic editor even in WordPress 5.x and higher. To make your custom post type use the new Gutenberg editor, you need to add 'show_in_rest' => true parameter to your code.

Custom Post Type

This is you normally write a function to create a custom post type in WordPress:

<?php
function risblco_register_post_types() {

	$args = array(

		'labels' => array(
            'name' => __( 'Reviews', 'text-domain' ),
            'singular_name' => __( 'Reviews', 'text-domain' )
        ),
        'has_archive' => true,
        'public' => true,
        'rewrite' => array('slug' => 'review'),

	);

	register_post_type( 'review', $args );

}
add_action( 'init', 'risblco_register_post_types' );
?>

This function will still use the classic editor in your post type, to make your post type uses the new Gutenberg editor you need to change it like this:

<?php
function risblco_register_post_types() {

	$args = array(

		'labels' => array(
            'name' => __( 'Reviews', 'text-domain' ),
            'singular_name' => __( 'Reviews', 'text-domain' )
        ),
        'has_archive' => true,
        'public' => true,
        'rewrite' => array('slug' => 'review'),
        'show_in_rest' => true

	);

	register_post_type( 'review', $args );

}
add_action( 'init', 'risblco_register_post_types' );
?>

Custom Taxonomy

What about custom taxonomy? You also need to add 'show_in_rest' => true to your custom taxonomies if you want your custom taxonomies work with Gutenberg editor.

Leave a Comment

Your email address will not be published. Required fields are marked *