How to Show Password-Protected Posts in WordPress Search Results

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;
}

FREE EBOOK
Your step-by-step roadmap to a profitable web dev business. From landing more clients to scaling like crazy.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

FREE EBOOK
Plan, build, and launch your next WP site without a hitch. Our checklist makes the process easy and repeatable.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

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.

Tags: