dongqie2010 2016-10-09 04:35
浏览 104
已采纳

修改搜索功能以排除帖子,并仅在前端将结果限制为每页10个

This problem is kind of two part. I have the solution to exclude posts from the search and limit the results to 10 results per page but the problem is it also affects the backend wp-admin area. So if i search for posts through the wp-admin area the posts will be excluded even though I only want this to occur for users who are not admin. This is the code I have for excluding posts and limiting the results to 10:

function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('post_type', 'page');
    }
    return $query;
}
add_filter('pre_get_posts','SearchFilter');

function change_wp_search_size($query) {
    if ( $query->is_search ) // Make sure it is a search page
        $query->query_vars['posts_per_page'] = 10;

    return $query;
}
add_filter('pre_get_posts', 'change_wp_search_size');

Now I have done a little bit of reading and research on stack and other sites and it looks like there is a way to only trigger these functions only after checking if admin is logged in. I'll reference to the wordpress codex for the is_admin function

This is the way I attempted to use the is_admin function which caused an http error 500. Please excuse me if the code is grossly incorrect:

if ( !is_admin() ) {
function SearchFilter($query) {
if ($query->is_search && !is_admin() ) {
$query->set('post_type', 'page');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
if ( !is_admin() ) {
function change_wp_search_size($query) {
if ( $query->is_search && !is_admin() )
$query->query_vars['posts_per_page'] = 10;
return $query;
}
add_filter('pre_get_posts', 'change_wp_search_size');

I'm hoping that I'm close and just need some tweaking for the code

Edit: So now this is what i have for both functions. I know its my php code which is the problem can you help further please:

function SearchFilter($query) {
if ( is_admin () ) {
return $query;
}
else {
($query->is_search) {
$query->set('post_type', 'page');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');

function change_wp_search_size($query) {
if ( is_admin () ) {
return $query;
}
if ( $query->is_search ) // Make sure it is a search page
$query->query_vars['posts_per_page'] = 10;

return $query;
}
add_filter('pre_get_posts', 'change_wp_search_size');

EDIT 2: Pastebin of the entire function.php file http://pastebin.com/tTEnHGp2

  • 写回答

2条回答 默认 最新

  • doucuo9126 2016-10-09 11:19
    关注
    function SearchFilter($query) {
        if ($query->is_search && !is_admin() ) {
            $query->set('post_type', 'page');
        }
    
    
        return $query;
    }
    
    add_filter('pre_get_posts','SearchFilter');
    
    function change_wp_search_size($query) {
        if ( $query->is_search && !is_admin() ) {
            $query->query_vars['posts_per_page'] = 10;
        }
    
        return $query;
    }
    
    add_filter('pre_get_posts', 'change_wp_search_size');
    

    This Should do it

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计