12

I have many custom post types that need the Add [custom post type] feature but I have a custom post type of "About" and I do not need to "Add New" about to the about custom post type. So I want to remove the button on top that says "Add About"

This is what I mean:

enter image description here

Any idea how I can remove that?

5 Answers 5

7

Please refer below :

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');
3
  • Thank really appreciate it, just one other question about the code. Can it be used to remove other elemnts on that same page? like for example the "Search About" to the rite. Nov 23, 2014 at 11:07
  • Yes, but for that you need to change necessary elements in code. Nov 24, 2014 at 5:13
  • This no longer works as it will give a "headers already sent" error message.
    – ZeroNine
    Dec 19, 2020 at 20:19
51

A prettier solution would be to disable the capability of creating a custom_post_type:

Simply pass the the parameter 'create_posts' => 'do_not_allow', in the capabilities array when calling register_post_type.

$args = array(
        'label'               => __( 'Custom Post Type', 'text_domain' ),
        'description'         => __( 'Custom Post Type', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'map_meta_cap'        => true,
        'capabilities' => array(
            'create_posts' => 'do_not_allow'
        )
    );
    register_post_type( 'custom_post_type', $args );
7
  • Is there a drawback to doing it this way? This seems like a much better solution than just hiding the links with CSS.
    – Nick Young
    Jun 10, 2015 at 3:01
  • 1
    Doesn't appear to work on 4.7.2 ... Besides. It's not even supposed to accept a boolean value. It should be a string value of: do_not_allow. Jan 27, 2017 at 18:52
  • 1
    not a preferred solution, sometime you should not be able to just create "posts" without a pre determined context. Somethings are just UX candy and the alternative of trying to give capabilities based on context in the admin is non maintainable nightmare. Jul 7, 2017 at 7:38
  • 1
    There's a related WP bug #22895 (core.trac.wordpress.org/ticket/22895) which causes the edit.php page for the CPT to not be shown if there would be only "All posts" menu item in the CPT admin submenu. There's a workaround for it at wordpress.stackexchange.com/questions/178033/….
    – jarnoan
    Sep 11, 2017 at 7:17
  • 1
    this works in 4.9.4 replacing 'create_posts' => false with 'create_posts' => 'do_not_allow'. And 'capability_type' => 'custom_post_type' can be removed, is not necessary Mar 10, 2018 at 14:13
4

Then, if you just want to remove Add New and you are not using custom_post_type, you should't use 'capability_type' => 'custom_post_type'. You'd better to remove this code. Good luck :)

So, the demo is below:

array(
'labels' => $member_labels,
'has_archive' => true,
'public' => true,
'hierarchical' => true, // like page
'rewrite' => array('slug' => 'member_pages'),
'supports' => array(
    'title',
    'editor',
    'excerpt',
    'custom-fields',
    'thumbnail',
    'page-attributes',
),
'taxonomies' => array('post_tag', 'cm_club'),
// 'capability_type' => 'custom_post_type',
'capabilities' => array(
    'create_posts' => false,
),
'map_meta_cap' => true,
)
3

Edited @TompaLompa's answer as it was incomplete. Adding create_posts => false won't work if map_meta_cap is not set to true.

Not setting this parameter (or setting it to false) will disable editing of the post type too. This is because you would need to add edit_post capability to each user role in order to add AND edit your post type.

Setting this parameter will use WP internal default meta capability handling and make it work for you if you don't need more finer control over role capabilities then the default WP ones.

-1

Best way i felt is install add-admin-javascript plugin and activate it then go to settings and add javascript write this code in last box

$('.wrap .wp-heading-inline').remove();
$('.wrap .page-title-action').remove();
$('#wp-admin-bar-new-content').remove();

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.