5ucms论坛

标题: 使 WordPress 的搜索结果包含加密或私密文章 [打印本页]

作者: admin    时间: 2021-5-29 17:28
标题: 使 WordPress 的搜索结果包含加密或私密文章
编辑当前主题下的 functions.php 文件,在 中间添加代码即可。

使未登录用户的搜索结果包含加密文章的代码

  1. add_filter( 'posts_search', 'include_password_posts_in_search' );
  2. function include_password_posts_in_search( $search ) {
  3.     global $wpdb;
  4.     if( !is_user_logged_in() ) {
  5.         $pattern = " AND ({$wpdb->prefix}posts.post_password = '')";
  6.         $search = str_replace( $pattern, '', $search );
  7.     }
  8.     return $search;
  9. }
复制代码

使登录用户的搜索结果包含私密文章的代码

  1. function include_password_posts_in_search( $query ) {
  2.     if ( is_user_logged_in() )
  3.         $query->set( 'post_status', array ( 'publish', 'private' ) );
  4.     }
  5. add_action( 'pre_get_posts', 'include_password_posts_in_search' );
复制代码

以上两个代码只能使用其中一个




欢迎光临 5ucms论坛 (http://bbs.5ucms.com/) Powered by Discuz! X3.2