Last updated on November 28, 2011. Tags: functions.php, WordPress search
A typical setup in a blog or any WordPress-powered website is to have a few static pages (usually simply referred to as "page") containing a few details about the site or the company, while the bulk of the content goes to the post pages (or just "posts").
Both pages and posts tend to appear in WordPress search results as long as they contain the searched keywords. I read somewhere that earlier versions of WordPress include only posts in search results. I'm not sure on what version did WordPress start to include pages in the search results, but when I started using WordPress back in version 2.3, this is already the case.
For a news website, the pages may contain information about the company owning the website, terms and conditions of using the site, and contact information, while the news articles are in the posts (one news article per post). For websites intended to showcase products, the setup is likely the same except that the each post is showcasing one product.
Pages are usually accessible in the navigation at all times, while old posts tend to get buried by new posts. They are accesible only through category pages, archives pages and search results. It is for this reason that you would likely want only the posts to appear in WordPress search results.
To remove all static pages from WordPress search results, open the theme editor and open theme functions (functions.php). Insert the following codes in the theme function.
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type','post');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
If there are scripts that are already in functions.php, the safest place to insert it is either immediately after the tag <?php or immediately before the tag ?> (it must always be inserted somewhere between these tags) to ensure that it will not interfere with these other scripts.
If there's no functions.php, simply create it by using the "new file" option in your hosting's control panel (if there's any) or by creating a blank text file functions.txt, replacing the extension name with .php and uploading it to your theme folder, in the same folder where all other theme files (index.php, single.php, etc) are placed.
There is an earlier tutorial on how to accomplish this. However, as one visitor (Karl Macklin) pointed out in the comment, this earlier method causes a problem, like creating a page with message "Search results for [keywords]" and nothing below it when the searched term is not in any of the posts but can be found on a static page. If no keywords really matched, WordPress should display the "No result found" message.
In the method discussed in that earlier tutorial, the search function simply skipped the static pages from the loop by not displaying it. For example, you configured your WordPress such that each search result page displays 10 results. Then, you entered a keyword, and the matching results (without the code that prevents the display of static pages) were 14 pages, 12 posts and 2 static pages, and the 2 static pages displays in page 1.
Then, supposed you implemented the earlier method, and repeated the same search. You would expect the search results to display 10 posts in the first search result page, and 2 posts in the second page. However, you will see that there are only 8 posts in the first page and still 4 posts in the second page. The search simply didn't display the pages, but stills counts them as results.
With this new method, the WordPress search will work as planned. Excluded pages are not counted at all. In the above example, if the method discussed in this article is implemented instead of the earlier method, the first search results page will display 10 posts, and the second page will display 2 posts. If the only page that contains the search keyword happens to be a static page, the "No results found" message will be displayed.
Posted by Greten on August 18, 2009 under WordPress
Comment Rules and Reminders
Posted by Tony Eldridge on 08.29.09 4:53 am
I am setting up a WordPress site now. Is there any way to prevent individual static pages from appearing in the WordPress search while allowing other ones to appear?
Thanks
Posted by Greten on 09.01.09 7:31 am
Hi Tony,
Now that you gave me the idea. Perhaps I will write on that for my next post.
Thanks
Addendum:
I cannot find a solution to this (excluding only certain pages) using the functions.php. My research on this will continue, but for now, my older method can help you. This post already stated the problem of that older method, but if you have less than 10 pages, the problem will not have noticeable impact on the performance of your site.
Posted by David Chambers on 09.14.09 9:55 am
Really useful information. Thanks for sharing this. I’m surprised that by default WordPress does not ignore pages while performing searches.