Wordpress - How do I remove the default post type from the admin toolbar
You need to hook to 3 different action hooks to fully hide the default post type. However, a direct access to the default post by URL is still possible. So, let's get started.
The Side Menu
add_action( 'admin_menu', 'remove_default_post_type' );
function remove_default_post_type() {
remove_menu_page( 'edit.php' );
}
The +New Post in Admin Bar
add_action( 'admin_bar_menu', 'remove_default_post_type_menu_bar', 999 );
function remove_default_post_type_menu_bar( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'new-post' );
}
The Quick Draft Dashboard Widget
add_action( 'wp_dashboard_setup', 'remove_draft_widget', 999 );
function remove_draft_widget(){
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
}
An alternative, but similar solution to Stefan and Rafa's. This does not throw errors, and notice if you navigate directly to /wp-admin/edit.php you will not be able to view the list of posts (default post type), or edit them.
Note this code does not actually disable the default post type, i.e. existing post types will still be published and accessible via public URL. It effectively disables backend access to the default post type for all users.
function remove_default_post_type($args, $postType) {
if ($postType === 'post') {
$args['public'] = false;
$args['show_ui'] = false;
$args['show_in_menu'] = false;
$args['show_in_admin_bar'] = false;
$args['show_in_nav_menus'] = false;
$args['can_export'] = false;
$args['has_archive'] = false;
$args['exclude_from_search'] = true;
$args['publicly_queryable'] = false;
$args['show_in_rest'] = false;
}
return $args;
}
add_filter('register_post_type_args', 'remove_default_post_type', 0, 2);
In principle it is possible to unregister already registered post types using unregister_post_type()
. Unfortunately this is not possible for _builtin
post types.
Alternatively it is possible to change the capabilities which are required to edit/create/delete/... posts for a specific post type. You could use the register_post_type_args
filter to change the capabilities which are required for the default post
post type. Setting all capabilities to false
will result in nobody having access to the default post post type. WordPress is smart enough to automatically hide the navigation entries.
add_filter('register_post_type_args', function($args, $postType){
if ($postType === 'post') {
$args['capabilities'] = [
'edit_post' => false,
'read_post' => false,
'delete_post' => false,
'edit_posts' => false,
'edit_others_posts' => false,
'publish_posts' => false,
'read' => false,
'delete_posts' => false,
'delete_private_posts' => false,
'delete_published_posts' => false,
'delete_others_posts' => false,
'edit_private_posts' => false,
'edit_published_posts' => false,
'create_posts' => false,
];
}
return $args
}, 0, 2);