Hide specific posts/pages/cpts from guest users in WordPress

Hide pages/posts/custom post types from guest users in WordPress. The setting can be done on per page/post basis.
If you wanted to hide just a couple pages from guest users, but want it to be available for members, you can use this plugin.

  1. Download this plugin.
  2. Upload and install it via wp-admin plugin installer. Activate the plugin.
  3. A checkbox will appear on edit page screen now:
    Restrict access custom field
    If you check the checkbox, this page will not be viewable unless user is logged in. Guest users will be redirected to login page when they try to view this page.
  4. By default, this checkbox only appears for ‘pages’ and not for ‘blog posts’ or any other custom post types that your website might have. It is however very easy to add it for other post types as well. For example, if you want it for custom post type called ‘course’, you can put the following code in your theme’s functions.php file:
    add_filter( 'restrict_access_post_types', 'myprefix_restrict_access_post_types' );
    function myprefix_restrict_access_post_types( $post_types ){
    	$post_types[] = 'course';
    	return $post_types;
    }

You can download the plugin from link below: