How to Show Password-Protected Posts in WordPress Search Results

By default, WordPress does not include password-protected posts in its search results. Here’s how you can change this behavior so that users can search through all of your posts.

Add this snippet of code to your theme’s functions.php file:

{code type=php}
add_filter( ‘posts_search’, ‘include_password_posts_in_search’ );
function include_password_posts_in_search( $search ) {
global $wpdb;
if( !is_user_logged_in() ) {
$pattern = ” AND ({$wpdb->prefix}posts.post_password = ”)”;
$search = str_replace( $pattern, ”, $search );
}
return $search;
}
{/code}

This tip comes to your courtesy of WPsnipp.com. Many thanks to Kevin Chard for posting this code. I tested it and it works as expected. Check out WPsnipp for many more handy code samples.