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?
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:
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:Extensive info on this can be found in the WordPress Codex.
Try this:
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:
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:
Maybe it is useful to somebody out there.