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

发布于 2024-12-05 08:22:56 字数 286 浏览 1 评论 0 原文

我创建了自定义页面模板。

<?php
/*
 * Template Name: foo
 */
?>

该文件名为“foo.php”。

我尝试过

global $query_string;
query_posts($query_string . "&post_type=post");

但所有页面都会被排除....

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

I created custom page template.

<?php
/*
 * Template Name: foo
 */
?>

This file name is "foo.php".

I tried

global $query_string;
query_posts($query_string . "&post_type=post");

But all pages will be excepted....

How to exclude only this page template from wordpress search results?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

十雾 2024-12-12 08:22:56

对于任何偶然发现此线程并且在 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');

For anyone whom stumbles on this thread and doesn't succeed on WP newer versions: the $query args must be set instead redoing query_posts... as the follows:

// 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');
清君侧 2024-12-12 08:22:56

Nicolay提到的查询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');

可以找到有关此内容的大量信息

The query mentioned by Nicolay is very handy, but it also removes all posts from the search results, because posts do not contain the '_wp_page_template' key. To have all pages (sans the filtered template) as well as all posts you need to do the following:

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

Extensive info on this can be found in the WordPress Codex.

卸妝后依然美 2024-12-12 08:22:56

试试这个:

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

Try this:

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 );
巴黎盛开的樱花 2024-12-12 08:22:56

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

就我而言,我想排除基于多个模板的内容,因此添加了更多键/值/比较数组元素。我也只想在搜索期间执行此操作,因此为此添加了一个条件子句。这是我添加到主题的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');

Thanks Nikolay! For some reason last night I just was not getting this to work but today, after another hour or two, I did. It may have simply been the fact that I was using the wrong filter or was missing the last line of your code.

In my case I wanted to exclude content based upon multiple templates, so, added more key/value/compare array elements. I also only wanted to do this during a search, so, added a conditional clause for that. Here's the complete function I added to my theme's functions.php file:

// 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');
遥远的绿洲 2024-12-12 08:22:56

我必须排除多个页面模板,所以我必须稍微调整上面的代码,但最终,这对我有用:

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

也许它对那里的人有用。

I had to exclude more than one page template, so I had to adapt the above code a little bit, but in the end, this worked for me:

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

Maybe it is useful to somebody out there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文