5ucms论坛
标题:
使 WordPress 的搜索结果包含加密或私密文章
[打印本页]
作者:
admin
时间:
2021-5-29 17:28
标题:
使 WordPress 的搜索结果包含加密或私密文章
编辑当前主题下的 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' );
复制代码
以上两个代码只能使用其中一个
欢迎光临 5ucms论坛 (http://bbs.5ucms.com/)
Powered by Discuz! X3.2