Remove default subpages from Custom Post Menu

I am creating a Custom Post Type using register_post_type. After running the function a new menu item appears in the Admin Panel.

Under this item there are menu items for new_item and all_items. Is there a way to get read of the these two options considering that I created a custom page to manage these posts.

Related posts

Leave a Reply

2 comments

  1. You can manipulate the global variable $submenu and remove them. In normal circumstances, the index key values are 5 (All items) and 10 (New item). In this example, the post type is portfolio:

    add_action( 'admin_init', 'remove_cpt_submenus_wpse_95797' );
    
    function remove_cpt_submenus_wpse_95797()
    {
        global $submenu;
        unset(
            $submenu['edit.php?post_type=portfolio'][5], 
            $submenu['edit.php?post_type=portfolio'][10] 
        );
    }
    
  2. Register the post type with show_ui set to false. Like this modified version of the sample in the Codex:

    function codex_custom_init() {
      $labels = array(
        'name' => 'Books',
        'singular_name' => 'Book',
        'add_new' => 'Add New',
        'add_new_item' => 'Add New Book',
        'edit_item' => 'Edit Book',
        'new_item' => 'New Book',
        'all_items' => 'All Books',
        'view_item' => 'View Book',
        'search_items' => 'Search Books',
        'not_found' =>  'No books found',
        'not_found_in_trash' => 'No books found in Trash', 
        'parent_item_colon' => '',
        'menu_name' => 'Books'
      );
    
      $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => false, // this is the change
        // 'show_in_menu' => true, // only works with show_ui true
        'query_var' => true,
        'rewrite' => array( 'slug' => 'book' ),
        'capability_type' => 'post',
        'has_archive' => true, 
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
      ); 
    
      register_post_type( 'book', $args );
    }
    add_action( 'init', 'codex_custom_init' );
    

    Note: With …

        'show_ui' => true,
        'show_in_menu' => false,
    

    … you do not get the menu item but do get the management page if you navigate to the link, so you can remove it from the menu but still have a functioning default edit system if you want.

    Edit:

    You said that you created a custom page to manage these posts. You must have also registered your own management page for that. I am not sure why you need the generated one. Put another way, I am not sure why you need to overhead of generating those default management pages only to remove the menus. However…

    function remove_add_new_menu_wpse_94776() {
        remove_submenu_page('edit.php?post_type=book','edit.php?post_type=book');
        remove_submenu_page('edit.php?post_type=book','post-new.php?post_type=book');
    }
    add_action('admin_menu','remove_add_new_menu_wpse_94776');
    

    That is copied from another thread, as you can see from the numeric suffix on the function name. The above can cause the whole menu to collapse to the top level only, under the right circumstances, unless there is some other menu in place, which presumable you have but for completeness here is a sort-of dummy menu.

    function remove_only_add_new_wpse_95797() {
      global $submenu;
      add_submenu_page(
        'edit.php?post_type=book',
        'test',
        'test',
        'edit_posts',
        basename('test'),
        'test_cb_wpse_95797'
      );
    }
    function test_cb_wpse_95797() {
      echo 'hi';
    }
    add_action('admin_menu','remove_only_add_new_wpse_95797');