shiro
shiro

Reputation: 83

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?

Upvotes: 8

Views: 5211

Answers (5)

Rinos Tenchos
Rinos Tenchos

Reputation: 11

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.

Upvotes: 1

Florian
Florian

Reputation: 800

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.

Upvotes: 5

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

Upvotes: 7

Jeff Lambert
Jeff Lambert

Reputation: 21

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

Upvotes: 2

Nikolay Yordanov
Nikolay Yordanov

Reputation: 1404

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

Upvotes: 4

Related Questions