1

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;
6

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.

4
  • Thanks, would you hide those just via CSS?
    – urok93
    Jul 12 '12 at 15:28
  • 1
    You might be better off removing the menu item by hooking onto admin_menu and searching for/unsetting the item in the global $submenu. As for the other items, check out the source & see if there's any suitable hooks/filters, otherwise fallback to CSS/JS. Jul 12 '12 at 15:43
  • Also how can I remove the Edit and Quick edit links
    – urok93
    Jul 12 '12 at 15:49
  • I don't have a menu item as I am adding the cpt to the menu via add_submenu_page
    – urok93
    Jul 12 '12 at 15:50
0

As an addition to the above/accepted answer, you could use this function to hide the actual links to the pages you want to disable.

remove_submenu_page( 
    'edit.php?post_type=product',
    'post-new.php?post_type=product' 
);
2
  • 4
    Please explain why that could solve the problem.
    – fuxia
    Mar 16 '13 at 22:42
  • It is part of the solution. the answer of TheDeadMedic make sure they won't be able to visit the add and edit page, this piece will the items from the admin menu. In this case, custom post type product
    – Frankey
    Nov 17 '14 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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