10

I have this custom post type:

function create_posttype() {
  register_post_type( 'companies',
    array(
      'labels' => array(
        'name' => __( 'شرکتهای عضو' ),
        'singular_name' => __( 'شرکت' )
      ),
      'supports' => array('title', 'editor', 'custom-fields', 'excerpt', 'thumbnail'),
      'public' => true,
      'has_archive' => true,
      'rewrite' => array('slug' => 'companies'),
    )
  );
}
add_action( 'init', 'create_posttype' );

Which shows classic editor in WordPress admin area. I tried to replace 'editor' with 'gutenberg' in the supports array which doesn't work. I also added this code to my function as suggested here:

add_filter('gutenberg_can_edit_post_type', 'prefix_disable_gutenberg');
function prefix_disable_gutenberg($current_status, $post_type)
{
    if ($post_type === 'companies') return true;
    return $current_status;
}

How can I have a Gutenberg editor on my custom post type?

18

For Gutenberg to work in a Custom Post Type you need to enable both the editor in supports (which you already have) and show_in_rest. So add 'show_in_rest' => true, to your post registration arguments array.

  • Glad it works, you are welcome. – Alvaro Dec 30 '18 at 12:50
1

Start by registering a Gutenberg WordPress custom type. The process is fairly easy and involves adding the following the code snippet.

/*Register WordPress  Gutenberg CPT */
function cw_post_type() {

    register_post_type( 'portfolio',
        // WordPress CPT Options Start
        array(
            'labels' => array(
                'name' => __( 'Portfolio' ),
                'singular_name' => __( 'Portfolio' )
            ),
            'has_archive' => true,
            'public' => true,
            'rewrite' => array('slug' => 'portfolio'),
            'show_in_rest' => true,
            'supports' => array('editor')
        )
    );
}

add_action( 'init', 'cw_post_type' );

add the show_in_rest key and set it to true via your custom post type.

'show_in_rest' => true,
   'supports' => array('editor')

As you can see, the above code snippet just set the ‘show_in_rest’ parameter to ‘TRUE’. After this step, when you create or edit a custom post type, you will see the Gutenberg editor visible and enabled.

All the steps and query discuss in detailed at https://www.cloudways.com/blog/gutenberg-wordpress-custom-post-type/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.