Home / functions.php / Include password protected posts in search results
  • How to Include password protected posts in search results

    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.

    wordpress snippet : 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;
    }
    

    ( WordPress codex functions, hooks, in this snippet. )

    posts_search, is_user_logged_in, add_filter, wp,