Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude wordpress page template(custom template) from search results?

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?

like image 779
shiro Avatar asked Sep 18 '11 17:09

shiro


People also ask

How do I exclude pages from search results in WordPress?

In this case, you may want to exclude all pages from search so that only blog posts show up in the search results. To do this, simply click the 'Sources & Settings' button. This opens a popup where you can uncheck the content that you want to exclude from WordPress search.

How do I exclude specific pages and authors from WordPress search?

To exclude from search results posts and pages made by specific authors, you'll have to add some custom code to your website. The two methods we'd recommend for this are creating a child theme and adding the code to its functions. php file or using the Code Snippets plugin without the child theme.

How do I exclude pages from WordPress?

To exclude a post or page from a WordPress search results page, simply check the relevant box and then publish or update the content. Now the post or page will be excluded from the search results on your WordPress site.


2 Answers

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');
like image 85
Edygar de Lima Oliveira Avatar answered Nov 14 '22 10:11

Edygar de Lima Oliveira


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.

like image 29
Florian Avatar answered Nov 14 '22 11:11

Florian