Php 如何从搜索结果中排除wordpress页面模板(自定义模板)?

Php 如何从搜索结果中排除wordpress页面模板(自定义模板)?,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,我创建了自定义页面模板 <?php /* * Template Name: foo */ ?> 但所有页面都将被排除在外 如何从wordpress搜索结果中仅排除此页面模板?尝试以下操作: global $wp_query; $args = array_merge($wp_query->query, array( 'meta_query' => array( array( 'key' => '_wp_page_t

我创建了自定义页面模板

<?php
/*
 * Template Name: foo
 */
?>
但所有页面都将被排除在外

如何从wordpress搜索结果中仅排除此页面模板?

尝试以下操作:

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 );

谢谢你,尼古拉!由于某种原因,昨晚我只是没能让它发挥作用,但今天,又过了一两个小时,我做到了。这可能只是因为我使用了错误的过滤器,或者缺少了代码的最后一行

在我的例子中,我希望排除基于多个模板的内容,因此,添加了更多的键/值/比较数组元素。我也只想在搜索过程中这样做,所以,为此添加了一个条件子句。下面是我添加到主题的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');

对于任何一个偶然发现此线程并且在WP更新版本上没有成功的人:必须设置$query args,而不是重做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');
上面提到的查询非常方便,但它也会从搜索结果中删除所有帖子,因为帖子不包含
“wp\u page\u模板”
键。要获得所有页面(无筛选模板)以及所有帖子,您需要执行以下操作:

// 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');

关于这方面的详细信息可以找到。

我不得不排除多个页面模板,因此我不得不稍微修改上面的代码,但最终,这对我来说是有效的:

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' => array('page-landings-new.php', 'page-landings-EU.php', 'page-thankyou.php'),
                    'compare' => 'NOT IN'
                ),
                // 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');

也许它对其他人有用。

谢谢尼古拉:我试试你的代码!但是…这个X不工作(但结果是一样的……嗯……这为我排除了页面模板,但也排除了所有帖子,因此只有页面在搜索中可见。我仍在努力寻找解决方案。我使用了Florian的解决方案以及Edygar对WP最新版本的
$query
的使用,它起到了作用。我将此解决方案与Edygar的编辑一起使用对于WP较新版本的
$query
,它成功了。
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' => array('page-landings-new.php', 'page-landings-EU.php', 'page-thankyou.php'),
                    'compare' => 'NOT IN'
                ),
                // 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');