|  | 
 
| 编辑当前主题下的 functions.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;
}
使登录用户的搜索结果包含私密文章的代码
 
 
 复制代码function include_password_posts_in_search( $query ) {
    if ( is_user_logged_in() )
        $query->set( 'post_status', array ( 'publish', 'private' ) );
    }
add_action( 'pre_get_posts', 'include_password_posts_in_search' );
以上两个代码只能使用其中一个
 | 
 |