WordPress: Disable “Add New” on Custom Post Type

前端 未结 10 918
执笔经年
执笔经年 2020-12-07 14:58

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

10条回答
  •  一生所求
    2020-12-07 15:06

    The combinations of the solutions above work in hiding the links (although someone could quite easily type the URL in directly.

    The solution mentioned @3pepe3 relies on get_post_type() which will only work if there is already a post in the listing. If there are no posts, the function will not return anything, and the "Add New" link will be available. An alternative method:

    function disable_new_posts() {
        // Hide sidebar link
        global $submenu;
        unset($submenu['edit.php?post_type=CUSTOM_POST_TYPE'][10]);
    
        // Hide link on listing page
        if (isset($_GET['post_type']) && $_GET['post_type'] == 'CUSTOM_POST_TYPE') {
            echo '';
        }
    }
    add_action('admin_menu', 'disable_new_posts');
    

    EDIT: To prevent direct access if someone types the URL in themselves: https://wordpress.stackexchange.com/a/58292/6003

提交回复
热议问题