I am making a plugin that allows admin to create custom post but I want admin to be able to create only one custom post type.

I tried this following to remove "Add New" from admin menu:

function remove_submenus() {
    $count= wp_count_posts('post_type')->publish;
    if( $count>=1 ){
        global $submenu;
add_action('admin_menu', 'remove_submenus');

It removes "Add New" item from the menu but user (admin) is still be able to create new custom post type by using this URL (/post-new.php?post_type=post_type).

How can I prevent user to create post more than one?

  • 1
    For some context, why are you implementing this? Context might provide information that leads to a superior solution that gives you what you want. Without context you might get an answer that does what you ask but has an obvious flaw. E.g. code that redirects when the user visits the add new page is useless if the admin uses the REST API to create the post, etc
    – Tom J Nowell
    Commented Apr 24, 2018 at 23:51

1 Answer 1

  1. Get the total number of custom types.
  2. If the total number of custom type is equal to 1 then throw an error message.

And put that custom post type making process in if else block.

if ($cpt_count == 1) {
  // Create new custom post type
} else {
  // Error

Your Answer

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

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