WordPress: Disable “Add New” on Custom Post Type

2019-01-30 10:27发布

Is there any way to disable the option of adding a new post under a Custom Post Type in WordPress (3.0)? I've looked into labels and arguments but can't find anything that would resemble such a feature.

标签: wordpress
10条回答
放荡不羁爱自由
2楼-- · 2019-01-30 11:10
register_post_type( 'custom_post_type_name', array(
  'capability_type' => 'post',
  'capabilities' => array(
    'create_posts' => false, // Removes support for the "Add New" function 
  ),
  'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
));

I hope it will help you. Many thanks !

查看更多
你好瞎i
3楼-- · 2019-01-30 11:14

Full credit to Seamus Leahy

There is a meta capability create_posts that is not documented but is used by WordPress to check before inserting the various 'Add New' buttons and links. In your custom post type declaration, add capabilities (not to be confused with cap) and then set it to false as below.

register_post_type( 'custom_post_type_name', array(
  'capability_type' => 'post',
  'capabilities' => array(
    'create_posts' => 'do_not_allow', // false < WP 4.5, credit @Ewout
  ),
  'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
));

May I ask why you want to do this?

I would at first have suggested changing the capabilities for your custom post type, but I don't think there's one that limits who can add posts, but only who can edit or publish them.

It looks a little dirty, but you could try unsetting the item in the $submenu global;

function hide_add_new_custom_type()
{
    global $submenu;
    // replace my_type with the name of your post type
    unset($submenu['edit.php?post_type=my_type'][10]);
}
add_action('admin_menu', 'hide_add_new_custom_type');

查看更多
干净又极端
4楼-- · 2019-01-30 11:14

@ Staffan Estberg,

This is best way to hide the Add New or Create New button in custom postypes

'capability_type'    => 'post',

        'capabilities'       => array( 'create_posts' => false ),       

        'map_meta_cap'       => true,

It disable to create new post in custom post types both side in admin menu and above the list of post type.

查看更多
等我变得足够好
5楼-- · 2019-01-30 11:17

There is a meta capability create_posts that is not documented but is used by WordPress to check before inserting the various 'Add New' buttons and links. In your custom post type declaration, add capabilities (not to be confused with cap) and then set it to false as below.

register_post_type( 'custom_post_type_name', array(
  'capability_type' => 'post',
  'capabilities' => array(
    'create_posts' => false, // Removes support for the "Add New" function ( use 'do_not_allow' instead of false for multisite set ups )
  ),
  'map_meta_cap' => true, // Set to `false`, if users are not allowed to edit/delete existing posts
));
查看更多
登录 后发表回答