首页 > 解决方案 > 如何从 Wordpress 搜索结果中排除特定页面

问题描述

当有人在我的网站中搜索时,我不希望包含一些页面。我试图将下面的代码添加到我的 function.php 但它不起作用

    add_filter( 'pre_get_posts', 'exclude_pages_search_when_logged_in' );
function exclude_pages_search_when_logged_in($query) {
    if ( $query->is_search && is_user_logged_in() )
        $query->set( 'post__not_in', array( 6410, 1684, 6385, 278, 6390, 865 ) ); 

    return $query;
}

如何从搜索结果中排除某些页面?

标签: wordpress

解决方案


add_action是你用来创建触发器“钩子”的东西——当有事情发生时,做其他事情。 add_filter用于“挂钩”数据更改/替换。改用下面的代码

add_action( 'pre_get_posts', 'my_search_exclude_filter' );
function my_search_exclude_filter( $query ) {
  if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
    $query->set( 'post__not_in', array( 9564, 1213  ) );
  }
}

我还描述了其他解决方案是我的博客文章https://naderzad.info/web-development/wordpress-search-result/


推荐阅读