Restrict custom post content to specific user

I’m not quite sure if I got you correctly.

However, if you have a 1:1 relationship between users and Student posts, you could do the following:

  • when a user registers, create a Student post
  • store the post ID as user meta data
  • use template_redirect for Student posts to check if the user is allowed to see the post, and redirect if not

Now, a basic approach to the above list in code:

function generate_student_post($id) {
    // get the User object
    $user = get_user_by('id', $id);
    // now you can use something like $user->first_name and the like

    // set up the post ...
    $post = array(
        'post_type' => 'student',
        // other post data
    );
    // ... and save to databse
    $post_id = wp_insert_post($post);

    // maybe add some meta data?
    add_post_meta($post_id, 'YOUR-META-KEY', 'YOUR-META-VALUE');

    // map the post to the user
    add_user_meta($id, 'student_post', $post_id, true);
} // function generate_student_post
add_action('user_register', 'generate_student_post');


function student_post_redirect() {
    global $post;
    if (
        // check if we're about to show a Student post ...
        'student' === $post->post_type
        && (
            // ... and shouldn't
            ! is_user_logged_in()
            || ! ($user = wp_get_current_user())
            || $post->ID !== get_user_meta($user->ID, 'student_post', true)
        )
    ) {
        wp_redirect(home_url());
        exit();
    }
} // function student_post_redirect
add_action('template_redirect', 'student_post_redirect');

Please note that this code is untested!

References