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?
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.
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.
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.
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');
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With