1

I have a CPT named 'book' and I would like to restrict front-end view of this book, like specific role users can only view this book contents from front-end, by default anyone can see CPT posts from frontend, I have tried with capabilities and map_meta_cap but not getting expected result.

CPT code:

$args = array( 
    'labels' => $labels,
    'hierarchical' => true,
    'description' => 'test book',
    'supports' => array( 'title', 'editor', 'author'),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'has_archive' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => array('book', 'books'),
    'capabilities' => array(
       'edit_post' => 'edit_book',
       'edit_posts' => 'edit_books',
       'edit_others_posts' => 'edit_others_books',
       'publish_posts' => 'publish_books',
       'read_post' => 'read_book',
       'read_private_posts' => 'read_private_books',
       'delete_post' => 'delete_book'
    )
);
register_post_type( 'book', $args );

Function:

      // "lite_user" is a custom user role to manage book
    $roles = array( get_role('lite_user') );
    foreach($roles as $role) {
      if($role) {
        $role->add_cap('read');
        $role->add_cap('edit_book');
        $role->add_cap('read_book');
        $role->add_cap('delete_book');
        $role->add_cap('edit_books');
        $role->add_cap('edit_others_books');
        $role->add_cap('publish_books');
        $role->add_cap('read_private_books');
      }
    }

by this code, any public can view/access these books from frontend but, I want to restrict this, lite_user role users can only able to view/access books from front-end.

1

You can filter the_content to make sure only people with the right cap can see it:

function filter_the_content( $content ) {

   // Check post type is book
   // and that the current user is not lite user
   if (get_post_type() == 'book' 
        && ! current_user_can( 'read_book' ) ) { 

        // return a message in place of the content
        return 'Why do you want to see this book? You can\'t read!';
    }

    // otherwise, show content
    return $content;

}

add_filter( 'the_content', 'filter_the_content' );

However!

This will only work for lite user, meaning that any other user (including admins) won't be able to see it from the front end.

You will have to add the read_book capability to other roles.

Hope that's what you are looking for.

  • Thanks, Pedro, it is working well, now I can restrict public view(guest), Actually I was looking for restriction through add_cap itself like as backend, Buth this also brilliant solution :) – Riyas Muhammed Jan 19 '17 at 16:11
  • 1
    Just thought of this: nother option is hooking into template_redirectto do the check and redirecting to a 404 Page. – Pedro Coitinho Jan 19 '17 at 18:33

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.