2011-09-18 213 views

回答

2

试试这个:

global $wp_query; 
$args = array_merge($wp_query->query, array(
    'meta_query' => array(
     array(
      'key' => '_wp_page_template', 
      'value' => 'foo.php', 
      'compare' => '!=' 
     ) 
    ), 
)); 
query_posts($args); 
+0

谢谢尼古拉:D 我试试你的代码!但是...不工作这个X( 但结果是一样的...嗯.. – shiro

1

感谢尼古拉!由于某种原因,昨晚我只是没有得到这个工作,但今天又过了一两个小时,我做到了。这可能仅仅是因为我使用了错误的过滤器或错过了代码的最后一行。

在我的情况下,我想基于多个模板排除内容,所以添加了更多的键/值/比较数组元素。我也只想在搜索过程中这样做,所以为此添加了条件子句。这是我加入到我的主题的functions.php文件的完整功能:

// exclude any content from search results that use specific page templates 
function exclude_page_templates_from_search($query) { 

    global $wp_the_query; 

    if (($wp_the_query === $query) && (is_search()) && (! is_admin())) { 

     $args = array_merge($wp_the_query->query, array(
     'meta_query' => array(
      array(
       'key' => '_wp_page_template', 
       'value' => 'page-template-1.php', 
       'compare' => '!=' 
       ), 
      array(
       'key' => '_wp_page_template', 
       'value' => 'page-template-2.php', 
       'compare' => '!=' 
       ), 
      array(
       'key' => '_wp_page_template', 
       'value' => 'page-template-3.php', 
       'compare' => '!=' 
       ) 
      ), 
     )); 

     query_posts($args); 

    } 

} 
add_filter('pre_get_posts','exclude_page_templates_from_search'); 
3

对于任何人在此线程绊倒和WP新版本不会成功:在$询问参数必须设置而不是重做query_posts .. 。为如下:

// exclude any content from search results that use specific page templates 
function exclude_page_templates_from_search($query) { 

    global $wp_the_query; 
    if (($wp_the_query === $query) && (is_search()) && (! is_admin())) { 

      $query->set(
       'meta_query', 
       array(
      array(
       'key' => '_wp_page_template', 
       'value' => 'page-template-1.php', 
       'compare' => '!=' 
      ) 
     ) 
    ); 
    } 

} 
add_filter('pre_get_posts','exclude_page_templates_from_search'); 
+0

这排除了我的页面模板,但也排除所有帖子,所以只有页面在搜索中可见。 –

+0

我使用了Florian的解决方案以及Edygar用于WP'$ query'的新版本,并且它能够正常工作 –

3

查询提到by Nicolay是非常方便的,但它也将删除搜索结果中的所有帖子,因为职位不包含'_wp_page_template'关键。要让所有页面(没有过滤的模板),以及你需要做以下的所有帖子:

// exclude any content from search results that use specific page templates 
function exclude_page_templates_from_search($query) { 
    global $wp_the_query; 
    if (($wp_the_query === $query) && (is_search()) && (! is_admin())) { 
     $meta_query = 
      array(
// set OR, default is AND 
       'relation' => 'OR', 
// remove pages with foo.php template from results 
       array(
        'key' => '_wp_page_template', 
        'value' => 'foo.php', 
        'compare' => '!=' 
       ), 
// show all entries that do not have a key '_wp_page_template' 
       array(
        'key' => '_wp_page_template', 
        'value' => 'page-thanks.php', 
        'compare' => 'NOT EXISTS' 
       ) 
      ); 
     $query->set('meta_query', $meta_query); 
    } 
} 
add_filter('pre_get_posts','exclude_page_templates_from_search'); 

这一广泛的信息可以发现in the WordPress Codex

+1

我使用了这个解决方案以及Edygar对WP新版本'$ query'的编辑,工作。 –