wordpress snippets last updated   : January 29, 2015
we now have 646 snippets

Include password protected posts in search results

wordpress snippet

Adding this snippet to the functions.php of your wordpress theme will allow protected posts to show up in search results even when a user is not logged in.

snippet :  PHPcopy
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;
}
source →

Did you like this post? If so please share it with your friends. Thanks!