1

我在 WordPress 中有一个具有父页面的页面。我想从 WordPress 搜索中排除这些父页面。

在functions.php中我试过这个:

function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_parent', '4');
}
return $query;
}

add_filter('pre_get_posts','SearchFilter');

好吧,这段代码只有 post_parent 是可搜索的,但我想要相反。这会是什么样子?

更新:问题解决了。这是解决方案(4 是要从搜索中排除父页面的特定页面的 ID):

function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('post_parent__not_in', array(4));
    }
    return $query;
}

add_filter('pre_get_posts','SearchFilter');

亲切的问候约翰

4

2 回答 2

2

使用 Wordpress 3.6,可以使用新的查询参数:post_parent__not_in

post_parent__not_in (array) - use post ids. Specify posts whose parent is not in an array.

于 2013-08-08T09:36:06.177 回答
0

在您的主题的 function.php 文件中使用此代码函数,并使用您想要从主题的自定义搜索栏中排除的页面 id...并享受它...!

// Exclude specific posts/pages from search
function exclude_pages_from_search($query) {    

   if ( $query->is_main_query() && is_search() ) { 

         $exclude_ids = array(11);// Array of the ID's to exclude   
         $query->set( 'post__not_in', $exclude_ids );   
        }   
           return $query; 
     } 

add_filter('pre_get_posts','exclude_pages_from_search' );
于 2017-12-26T11:03:13.993 回答