Include password protected posts in search results

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.

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;

Comments  Leave a Reply

  1. Very nice, it’s just I wanted

  2. Daily Tip: How to Show Password-Protected Posts in WordPress Search Results October 17, 2011 at 1:03 pm

    […] 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 […]

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!