How to enable Gutenberg block editor for custom post type in WordPress

Hello Gutenberg! Yes, This is first of my post with Gutenberg! that’s a very good editor for authors and I love them because I got good feeling at first.

Enable in post types

 For other post types, if you want to enable the Gutenberg, just make sure the show_in_rest argument is true in your post type setting.

function book_post_type() { 
    $args = array(
        'label'              => 'Book',
        'public'             => true,
        'rewrite'            => array( 'slug' => 'book' ),
        'capability_type'    => 'post',
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
        'show_in_rest'       => true,
    );
 
    register_post_type( 'book', $args );
}
 
add_action( 'init', 'book_post_type' );

Disable in post types

It’s okay if that argument is false, but for making sure that is disabled, you can use the below hook.

function post_type_filter( $use_block_editor, $post_type ) {
if ( 'books' === $post_type ) {
return false;
}

return $use_block_editor;
}
add_filter( 'use_block_editor_for_post_type', 'post_type_filter', 10, 2 );