How do I disable adding of new posts of a particular custom post type?
Found this code in Log Deprecated Notices plugin:
$screen = get_current_screen();
if ( self::pt == $screen->id && ( $screen->action == 'add' || $_GET['action'] == 'edit' ) )
wp_die( __( 'Invalid post type.', 'log-deprecated' ) );
if ( self::pt != $screen->post_type )
return;
As per this answer, the “correct way” is:
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
));
—
add_action( 'load-post-new.php', 'wpse_58290_disable_new_post' );
function wpse_58290_disable_new_post()
{
if ( get_current_screen()->post_type == 'my_post_type' )
wp_die( "You ain't allowed to do that!" );
}
Note that you’ll also need to hide the UI elements, such as in the menu, and the “Add New” button in the h2
tag on the edit screen.