76

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.

1
  • 2
    Talk about digging up the dead - I stumbled back here today & I can see Seamus' answer, you should consider marking it as accepted, it's definitely the "right" one. Dec 1, 2015 at 16:45

9 Answers 9

132

There is a meta capability create_posts that is documented here and 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
));

You'll probably want to set map_meta_cap to true as well. Without it, you won't be able to access the posts' editing pages anymore.

11
  • 4
    This will also remove the read/edit/trash functions. The user can only see the list of posts but not open or edit anything. To allow users to edit existing posts you also have to add the option 'map_meta_cap' => true
    – Philipp
    Jan 26, 2014 at 14:28
  • 7
    Instead of setting create_posts to false you can set it to another capability such as administrator to only allow certain roles to create posts.
    – Tamlyn
    Nov 5, 2014 at 12:03
  • 7
    I had to change the snippet to 'create_posts' => 'do_not_allow' to make it work for multisite setups - but nevertheless thank's for the insights!
    – s1lv3r
    Jul 23, 2015 at 17:10
  • 7
    'create_posts' => false no longer works in WP4.5. I solved this by using 'create_posts' => 'do_not_allow' (a non-existing capability) instead @seamus-leahy, perhaps you'd like to update your answer?
    – Ewout
    Apr 28, 2016 at 8:51
  • 2
    Works perfectly for administrator. I need my subscribers to be able to edit their existing posts but it disables them even from viewing posts' page. Any solution? Jun 11, 2018 at 4:00
13

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 '<style type="text/css">
        #favorite-actions, .add-new-h2, .tablenav { display:none; }
        </style>';
    }
}
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

0
8

Disable creating new post for registered post-types: (example for post and page)

function disable_create_newpost() {
    global $wp_post_types;
    $wp_post_types['post']->cap->create_posts = 'do_not_allow';
    //$wp_post_types['page']->cap->create_posts = 'do_not_allow';
    //$wp_post_types['my-post-type']->cap->create_posts = 'do_not_allow';
}
add_action('init','disable_create_newpost');
1
  • Thank you - this is great. We are using this and targeting wp_block and using 'administrator' to stop non-admins from adding new synced patterns via wp-admin/edit.php?post_type=wp_block (what used to be reusable blocks until version 6.3) Aug 11 at 8:40
8

WordPress Networks: I found that Seamus Leahy's answer doesn't work if you are logged in as a super admin of the network, it doesn't matter if the user doesn't have the capability, mapped or otherwise, when current_user_can($cap) is called by the CMS. By digging into the core I found you can do the following.

register_post_type( 'custom_post_type_name', array(
  'capability_type' => 'post',
  'capabilities' => array(
    'create_posts' => 'do_not_allow', // Removes support for the "Add New" function, including Super Admin's
  ),
  'map_meta_cap' => true, // Set to false, if users are not allowed to edit/delete existing posts
));

The accepted answer hides the menu item, but the page is still accessible.

2
  • this also prevents me from displaying the list of my custom post type. is there any solution for this? Nov 25, 2021 at 12:50
  • This is definitely working!
    – Ahmad Wael
    Jan 3 at 18:04
7

In wordpress and for all the post types there is the capability create_posts. This capability is used in several core files :

  1. wp-admin\edit-form-advanced.php
  2. wp-admin\edit.php
  3. wp-admin\includes\post.php
  4. wp-admin\menu.php
  5. wp-admin\post-new.php
  6. wp-admin\press-this.php
  7. wp-includes\admin-bar.php
  8. wp-includes\class-wp-xmlrpc-server.php
  9. wp-includes\post.php

So if you really want to disable this feautere you must do it per role and per post type. I use the great plugin "User Role Editor" to manage the capabilities per role.

But what about the capability create_posts? Well this capability is not mapped and also create_posts is equal to create_posts so we should fix this and map the capability per post type.

So you can add this piece of code in your functions.php and the you can manage this capability.

function fix_capability_create(){
    $post_types = get_post_types( array(),'objects' );
    foreach ( $post_types as $post_type ) {
        $cap = "create_".$post_type->name;
        $post_type->cap->create_posts = $cap;
        map_meta_cap( $cap, 1); 
    }
}
add_action( 'init', 'fix_capability_create',100);

So here we are not hiding or removing menu elements... here we are removing the capability for users (including xmlrpc requests).

The action was init and not admin_init or anything else because init at priority 100 prevents the display of "add new" on admin bar, sidebar, etc (in all the wp interface).

0
6
add_action("load-post-new.php", 'block_post');

function block_post()
{
    if($_GET["post_type"] == "custom_type") 
        wp_redirect("edit.php?post_type=custom_type");
}
0
4

@ 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.

1
  • Hi I want disable edit and move to trash/remove custom post type in Wordpress 6.0.2 (admin should be able to create/publish, and everyone should be able to read). I try by 'delete_post' => 'do_not_allow', 'edit_post' => 'do_not_allow' or false but it doesn't work, because it makes add new imposible too. Had you any idea how to do that?
    – kris016
    Oct 2, 2022 at 19:51
1

I found this simplest way for this. Just ad this code into theme’s function.php.

function hd_add_buttons() {
    global $pagenow;
    if (is_admin()) {
        if ($_GET['post_type'] == 'custom_post_type_name') {
            echo '<style>.add-new-h2{display: none !important;}</style>';
        }
    }
}
add_action('admin_head', 'hd_add_buttons');
1
  • 1
    Be aware, this simply hides the 'add new' button, it does not programmatically prevent users from adding new posts.
    – Ben
    May 20, 2018 at 17:54
1

As the question is 'how to disable add-new button on custom post type', and not 'how to restrict user editing custom post types', in my opinion the answer should be purely hiding the buttons with css, by adding this to the functions.php file :

add_action( 'admin_head', function(){
    ob_start(); ?>
    <style>
        #wp-admin-bar-new-content{
            display: none;
        }
        a.page-title-action{
            display: none !important;
        }
        #menu-posts-MY-CUSTOM-POST-TYPE > ul > li:nth-child(3) > a{
            display:none;
        }
    </style>
<?php ob_end_flush();
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.