How do I remove all the metaboxes for a custom post type?

What’s an effective way of removing all the meta boxes for a specific post type in WordPress?

The only solution for removing metaboxes at all that I’ve found seems to be the remove_meta_box function, which requires an id of the metabox to be removed. I could remove all the default metaboxes like this, it would be a little fiddly but not impossible or even hard.

Read More

However, how would I go about consistently removing the meta boxes added plugins or theme functions elsewhere? Those are dynamic and unpredictable, maybe I can reliably get a summary of the meta boxes for a custom post types edit page and maybe work from there?

Thanks,
B

Related posts

Leave a Reply

2 comments

  1. You can do this in one of two ways –

    1. Brutally remove all metaboxes without any kind of check.
    2. Loop through each metabox individually, which will allow you to check the ID and keep some if you wish.

    My personal preference would be method 2, although there will of course be some overhead compared to the more brutal method 1.

    Using PHP microtime, the speeds are recorded as follows –

    • Method 1 – 0 (so very, very fast).
    • Method 2 – 0.00099992752075195 (pretty darn fast, but obviously slower).

    Method 1 – quick and brutal –

    add_action('add_meta_boxes', 'my_remove_meta_boxes1', 99, 2);
    function my_remove_meta_boxes1($post_type, $post){
    
        global $wp_meta_boxes;
    
        /** Simply unset all of the metaboxes, no checking */
        unset($wp_meta_boxes[$post_type]);
    
    }
    

    Method 2 – slow and careful –

    add_action('add_meta_boxes', 'my_remove_meta_boxes2', 99, 2);
    function my_remove_meta_boxes2($post_type, $post){
    
        /** Check the post type (remove if you don't want/need) */
        if(!in_array($post_type, array(
                                     'post',
                                     'page'
                                 ))) :
            return false;
        endif;
    
        global $wp_meta_boxes;
    
        /** Create an array of meta boxes exceptions, ones that should not be removed (remove if you don't want/need) */
        $exceptions = array(
            'postimagediv'
        );
    
        /** Loop through each page key of the '$wp_meta_boxes' global... */
        if(!empty($wp_meta_boxes)) : foreach($wp_meta_boxes as $page => $page_boxes) :
    
                /** Loop through each contect... */
                if(!empty($page_boxes)) : foreach($page_boxes as $context => $box_context) :
    
                        /** Loop through each type of meta box... */
                        if(!empty($box_context)) : foreach($box_context as $box_type) :
    
                                /** Loop through each individual box... */
                                if(!empty($box_type)) : foreach($box_type as $id => $box) :
    
                                        /** Check to see if the meta box should be removed... */
                                        if(!in_array($id, $exceptions)) :
    
                                            /** Remove the meta box */
                                            remove_meta_box($id, $page, $context);
                                        endif;
    
                                    endforeach;
                                endif;
    
                            endforeach;
                        endif;
    
                    endforeach;
                endif;
    
            endforeach;
        endif;
    
    }
    
  2. David has given you good options. I just used the following for your specific use-case: (Replace your_custom_post_type with the name of your CPT)

    add_action( 'add_meta_boxes', 'test_remove_metaboxes', 5 ); // hook early and remove all metaboxes
    
    function test_remove_metaboxes(){
        global $wp_meta_boxes;
        global $post;
        $current_post_type = get_post_type($post);
        if($current_post_type == 'your_custom_post_type') {
            $publishbox = $wp_meta_boxes['your_custom_post_type']['side']['core']['submitdiv'];
            $wp_meta_boxes = array();
            $wp_meta_boxes['your_custom_post_type'] = array(
                'side' => array('core' => array('submitdiv' => $publishbox))
              );
        }
    }
    

    And then you can add your own meta-boxes as you like.