Exclude Protected Items from Search Results

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.

Source.

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.