You might sometimes want to redirect to Login page or to a specific page when guest users try to access certain pages, posts or categories in WordPress. The following function directs non-logged in users to a specific page:
add_action( 'template_redirect', 'redirect_to_specific_page' ); function redirect_to_specific_page() { if ( is_page('slug') && ! is_user_logged_in() ) { wp_redirect( 'http://www.example.com/your-page/', 301 ); exit; } } // Source: http://wordpress.stackexchange.com/
You can specify the slug of the page which will be access-restricted by adding the actual slug in "is_page('slug')". You can use array to specify multiple pages like: is_page( array( 42, 'about-me', 'Contact' ) ) (See here). Of course, you can replace is_page with is_category (Category), in_category (Posts within a category) or is_single)(Single posts).
In the above function, guest users will be redirected to a specific page. You can also move the users to WordPress Login page by replacing the redirected URL with wp_login_url( home_url() ). Then, non-logged in users will be redirected to Login Page and then to home page after entering login information. If you want to move them to the previous page, please use wp_login_url( get_permalink() ) instead of wp_login_url( home_url() ) (Refer to wp_login_url).