Jul
08
2010
Excluding Pages from WordPress Search
When people visit my site and do a search, I assume they’re looking for results within my posts and not in any of my static pages. Too bad WordPress automatically searches through both. In order to exclude pages from the WordPress search, you need to add a small piece of code to the functions.php file. I found a solution on WP Recipes but I was not a fan of having to add all of my category IDs in order to make it work. So I decided to modify it a bit.
function SearchFilter($query) { if ($query->is_search) { $query->set('post_type', 'post'); } return $query; } add_filter('pre_get_posts','SearchFilter'); |
I forced it to just search for posts through setting the post_type. You can also make it do the opposite by setting the post_type to page.



Thank your for sharing…
I have re-written your post in Bahasa Indonesia on my blog about this tips and surely the credit goes to you
Very nice Post i like it
hi me and my friend have tried to connect your rss feed but i got an site error. Ensure you have look and handle the error as pal certainly like to Add your blog site to my rss news feed. By the way very good web site you got here. Greetings
Have there been some changes with WordPress 3.0? I have moved this snippet into the themes functions.php file and it does not work.
If I add this code within the while (search) loop only the posts are then returned.
if ($query->is_search) {
$query->set('post_type', array( 'post', 'classified' ));
}
I am using the Twenty Ten theme. Thanks is advance.
The whole code snipped above needs to be added to the functions.php file outside of every other function and between PHP tags. I use it on multiple sites and it works, even in 3.1.
Yes work with WordPress is not very convenient. Recently, I encountered the same problem. Thank you for your advice and your help.