WordPress.org

Support

Support » Plugins and Hacks » Page visibility private displays 404 error

Page visibility private displays 404 error

Viewing 3 replies - 1 through 3 (of 3 total)
  • I’ve tried adding the code above and the code below to the page loop right after <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    The code below still returns the 404 error. Am I putting the code in the wrong place?

    <?php
    	$slug = basename($_SERVER['REQUEST_URI']);
    	$query = 'SELECT * FROM wp_posts WHERE post_name = "'.$slug.'" LIMIT 1';
    	$page_query = $wpdb->get_results($query, OBJECT);
    	$private = ( is_object($page_query[0]) && $page_query[0]->post_status == 'private' );
    ?>
    
    <?php if ( !$private ) { ?>
    	<h1>Error 404 - Page Not Found</h1>
    	<p>Please use the main navigation or the back button on your browser to find what you're looking for.</p>
    
    <?php } else { ?>
    	<h1>Authorization required - please log in.</h1>
    	<?php get_template_part('login'); ?>
    
    <?php } ?>

    I also tried adding the code in the second post to my 404.php file. It just displays

    Error 404 – Page Not Found
    Please use the main navigation or the back button on your browser to find what you’re looking for.

    latro666

    @latro666

    Hi there, Bit of thread necromancy but it should help searchers as i needed to do this and came on this thread.

    I worked out a way of doing this myself after much head scratching, its not very elegant but should do the trick. Its along the same lines as above.

    The 404 template does not provide you with a queried post to work with if the post is private but WordPress still does do all the query string and SQL formulation for you! So you can in effect re-query this and check the post thus:

    <?php
    $requery = $wpdb->get_results($wp_query->request);
    
    if(count($requery) == 1 && $requery[0]->post_status == 'private'){
        //we have a single post and it is private
        wp_login_form();
    }else{
        //data empty or not private just kick out a normal 404
        echo("404 page not found");
    } ?>

    Just plop that in 404.php in your main content div and should work.

    You might wanna apply some args to wp_login_form to redirect to said page after login. http://codex.wordpress.org/Function_Reference/wp_login_form

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Page visibility private displays 404 error’ is closed to new replies.
Skip to toolbar