【问题标题】:Prevent WordPress search from search a specific pages parentpages阻止 WordPress 搜索搜索特定页面的父页面
【发布时间】:2013-08-08 08:56:10
【问题描述】:

我在 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');

亲切的问候 约翰

【问题讨论】:

    标签: wordpress search


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      在您的主题的 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' );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-10
        • 1970-01-01
        • 2016-09-01
        • 2011-05-16
        • 2012-08-06
        • 1970-01-01
        相关资源
        最近更新 更多