I have created a membership site which is working how I would like in that non-members cannot view private pages.

However, I want that when someone who has not logged in, clicks on a link that links to a private page, they should be directed to my login page.

The closest I can figure out is using post_password_required(), but it doesn't seem to be working.

I don't want to hide the links from non-members.

function redirect_to_login() {
    if( post_password_required( get_the_ID() )  ) {
        wp_redirect( "index.php?p=50" );
add_action('wp', 'redirect_to_login');

3 Answers 3


The function you are using-- post_password_required-- is true for password protected pages, not for private pages. I am honestly not sure which you mean, but if you do mean "private" as in most of your description then...

I am not aware of an is_private function but you can check the post object itself. There is a catch though. "Private" posts are rabidly protected. A user without permissions to see the post will get a 404. I think the following will work, but due to the nature of "private" posts this may still be buggy. It has not been rigidly tested.

function redirect_to_login() {
  global $wp_query,$wpdb;
  if (is_404()) {
    $private = $wpdb->get_row($wp_query->request);
    if( 'private' == $private->post_status  ) {
add_action('template_redirect', 'redirect_to_login');
  • You are right, I mean pages that have the status set as 'private'. I tried the code you suggested in the functions.php file but it produces a white screen. When a non-logged in user clicks on a link to a 'private' page, currently the 404 page is showing. So I need to intercept this before the user is taken to the 404.
    – tommyf
    Jul 2, 2013 at 15:58
  • @tommyf : See the edit.
    – s_ha_dum
    Jul 2, 2013 at 16:38

I had exactly the same problem and I found a quite neat solution for it. When investigating, how Wordpress exactly handles the "Private-404" I found, that the post in question is in the queried_object. So you can actually check for that. Therefore in your 404.php do the following:

$queried = get_queried_object();
if (is_a($queried, 'WP_Post') && $queried->post_status == 'private') {
    // Set correct header
    header('HTTP/1.0 403 Forbidden', true);
    // Show login form here or redirect to 
} else {
    // show 404-page

Try something like this in your header or appropriate template file:

$not_auth_redirect = get_permalink(8); // some page's id
if(!is_user_logged_in() && post_password_required(get_the_ID()) {
    wp_redirect( $not_auth_redirect );

You can set an ID to your home-page or some call to action page, then have the people redirected there if they are not logged in AND it is a password-required post.

  • Thanks, it seems I was going about it the wrong way. However I'm still having issues, the if statement isn't being triggered when it should & I think it's to do with the post_password_required. There are no posts only pages made private, does it also apply to pages as well as posts?
    – tommyf
    Jul 2, 2013 at 13:48
  • No, just the one function, which should work for pages as well. Try to trigger the condition on something else to see if it works. Like if(1 == 1)
    – GhostToast
    Jul 2, 2013 at 13:51
  • Yes if(1 == 1) does trigger it. Also if I just use !is_user_logged_in() it triggers, which makes me think there's a problem with post_password_required()
    – tommyf
    Jul 2, 2013 at 13:54
  • Oops. I think we need the post's ID there. Try post_password_required(get_the_ID())
    – GhostToast
    Jul 2, 2013 at 13:58
  • Unfortunately even with get_the_ID() it doesn't work. I think the solution cannot work from the head as the user has already been redirected to the 404 page before any of the 'private' page is output. I have to intercept the link click to the private page and take the user to the login page, presumably via functions.php.
    – tommyf
    Jul 2, 2013 at 16:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.