July 03, 2019
Password Protected items show up in Search Results for logged in Users. This can be confusing when linking to those items for users without accounts.
Limit the search results to exclude Password Protected Posts / Pages with the following:
// hide protected posts
function exclude_protected($where) {
global $wpdb;
return $where .= " AND {$wpdb->posts}.post_password = '' ";
}
// conditional logic for exclusions
function exclude_protected_action($query) {
if( is_search() &! is_admin() ) {
add_filter( 'posts_where', 'exclude_protected' );
}
}
add_action('pre_get_posts', 'exclude_protected_action');
Be sure to add !is_admin to the search filter so that searching in the backend is not affected by the filter.