Hide/show password protected posts on specific pages

This goes into functions.php

function exclude_protected($where) {
global $wpdb;
return $where .= " AND {$wpdb->posts}.post_password = '' ";
}

// Decide where to display them
function exclude_protected_action($query) {
if( !is_single() && !is_archive() && !is_admin() ) {
add_filter( 'posts_where', 'exclude_protected' );
}
}

// Action to queue the filter at the right time
add_action('pre_get_posts', 'exclude_protected_action');

Leave a Reply

Your email address will not be published. Required fields are marked *