WordPress – Exclude pages from search results
One way to exclude pages from search results is to install the Simply-Exclude plugin. Another solution I found to work pretty well is to add a filter in functions.php to only include posts categories instead of excluding pages from search results:
1 2 3 4 5 6 7 8 9 10 | // Remove pages from search function mySearchPostsFilter($query) { if ($query->is_search) { $query->set('cat','12,14,16,17,18'); // your category IDs } return $query; } add_filter('pre_get_posts','mySearchPostsFilter'); |
That’s it! Replace the posts category IDs (’12,14,16,17,18′) by your owns and it should work.
The Author
Want to become a guest author on this blog?
jason allen says:
26 Nov, 2008
how do you exclude categories or posts?
I have found many plugins that will easily exclude pages. I’m looking for a pluglin to exclude posts.
know anything for that?
Scott says:
26 Sep, 2011
This page could be usefull. It shows how to exclude pages or include specific categories in WordPress search
Scott says:
26 Sep, 2011
Sorry …
http://tocs-i.com/blog/exclude-pages-or-include-specific-categories-in-wordpress-search/
Jeremie Tisseau says:
26 Nov, 2008
Well, this post explains how to exclude categories. To exclude posts, once I came across a plugin but I don’t remember its name.
Have you tried to search the WordPress plugin directory?
Otherwise, ask on their forum. Someone might answer you. you can also search on Lorelle’s site. He always tells about the latest plugins.
jasprit says:
13 May, 2009
sir, i am having problem in my search it shows only the post name but i want like the as it shows in the index page of my site systematically
Wouter de Winter says:
23 Jul, 2009
Instead of including categories you can also specify a post type. This example includes only posts and excludes pages:
$query->set(‘post_type’, ‘post’);
Jeremie Tisseau says:
27 Jul, 2009
Thanks Wouter for the tip.
William Knight says:
9 Jun, 2010
Brilliant little function hack! Just what I needed…thanks Jeeremie!
Walt Ribeiro says:
23 Sep, 2010
Jeremie, great article. I was wondering – how can I search a term in ONLY post title as opposed a whole category or blog post? For example, if I specify ‘Bon+Jovi as the term for a category its written as http://fororchestra.com/category/songs/?s=Bon+Jovi – but what if I want to search ONLY a blog post title?