• Hi all,

    I am trying to restrict access to custom post type post pages, redirecting the user to the login page if they are not logged in.

    I have tried using the access settings in the plugin using the ‘Global Site Access’ and then adding the pages the user can view when logged out.

    Although this works, it only works some times. When you click on some posts it does work and other posts it doesnt (logged in user gets redirected to either the post or their profile page).

    I am currently trying a different method – making the site accessible to everyone in ‘Global Site Access’ and using the following in my child theme functions.php:

    add_action( 'template_redirect', 'redirect_to_specific_page' );
    
    function redirect_to_specific_page() {
    
    if (! is_user_logged_in() && (is_singular(array('talks','interviews','articles'))) ) {
    
    wp_redirect( 'https://cybertalks.co.uk/login/', 301 ); 
      exit;
        }
    }

    Could you tell me if this is the right & best way to achieve this?

    Thank you!

    Note you can use the following:

    email: [email protected]
    password: password

    The page I need help with: [log in to see the link]

  • The topic ‘Help! Restricting access to custom post types does not always work.’ is closed to new replies.