I've created a custom post type called Students with custom fields like ID #, Name, year, age, etc. With this, I have also created one "Student" post for each student in a school.

What I want to do is to restrict each post to a single student by ID #, with the student being able to login and view their page (they can't edit, just view) on the frontend.

I've tried User Specific Content, Role Scoper, and other plugins but none of them seem to work in this scenario.

Pls suggest whatever you can that may help me.

Thanks in advanced.

1 Answer 1


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)
    ) {
} // function student_post_redirect
add_action('template_redirect', 'student_post_redirect');

Please note that this code is untested!


Your Answer

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