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

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.

Free Video Why 100 is NOT a Perfect Google PageSpeed Score (*5 Min Watch) Learn how to use Google PageSpeed Insights to set realistic goals, improve site speed, and why aiming for a perfect 100 is the WRONG goal.
Tags: