WordPress displays private posts to logged-in users -- how to turn this functionality off? WordPress displays private posts to logged-in users -- how to turn this functionality off? wordpress wordpress

WordPress displays private posts to logged-in users -- how to turn this functionality off?


The hack way to do what you want is to put this line of code at the top of your loop (after the the_post() part:

if( get_post_status()=='private' ) continue;

This is the hack way because your WordPress is still loading that post from the database and factoring it in to post counts, etc, but skipping it when going to display it. If you searched for a phrase that was only in private posts, you would get a blank page without any error, for example.

The correct way to do this is to add a filter that modifies the SQL used to generate the list of posts. The tricky part is to not filter it if you're in the admin section, otherwise you'll never see your private posts again. The best place for this filter is in your theme's functions.php file. Here's what you should put in there:

add_filter('posts_where', 'no_privates');function no_privates($where) {    if( is_admin() ) return $where;    global $wpdb;    return " $where AND {$wpdb->posts}.post_status != 'private' ";}


Why not just add 'post_status' => 'publish' to the WP_Query args?

$the_query = new WP_Query( array(    'post_type' => 'post' ,    'orderby' => 'date' ,    'order' => 'DESC' ,    'post_status' => 'publish',    'posts_per_page' => 6,) );


So if no one is to view these private posts, including admins, why not just set their status to unpublished or draft?