Custom Post Type, limit to one

I’m using custom post types for a few pages where I have a very customized template…but I only need one of each post (maybe not the best way to go about this…but it’s built so I’d like to not have to change this).

Is there a way to remove the “Add New” link on the left side admin menu just so the user doesn’t keep making more than 1 version of this post?

Read More

I’m okay with the “Add new” inside the actual page as that seems a bit harder to find.

Related posts

Leave a Reply

1 comment

  1. You can remove the ‘Add new’ submenu page like so:

    add_action( 'admin_menu', 'myprefix_adjust_the_wp_menu', 999 );
    function myprefix_adjust_the_wp_menu() {
      //Get user id
      $current_user = wp_get_current_user();
      $user_id = $current_user->ID;
    
      //Get number of posts authored by user
      $args = array('post_type' =>'myposttype','author'=>$user_id,'fields'>'ids');
      $count = count(get_posts($args));
    
      //Conditionally remove link:
      if($count>1)
           $page = remove_submenu_page( 'edit.php?post_type=myposttype', 'post-new.php?post_type=myposttype' );
    }
    

    A similar logic can be used to conditionally redirect users from the ‘add new’ page if they have already created a post.

    This method would also support querying by post status (i.e. they can only have at most one published post).

    Of course, the above only removes the link to the ‘add new’ page – it doesn’t actually stop them creating posts. Depending on what you are after, you could remove the ‘publish post’ capability when they have more than 1 post (they will still see the add new link, and could create drafts but they won’t be able to publish). If you do this you’ll need to add the capability back when they ‘unpublish’ their post.