November 1, 2008

All, PHP, Tutorials, WordPress

WordPress - Exclude pages from search results

Article written by Jeeremie

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

Article written by Jeeremie:

Hi, My Name is Jeremie Tisseau. I am a French UI/UX Designer, Event Organizer and Web Entrepreneur based in Bangkok, Thailand, since January 2009. I design beautiful and functional web and mobile apps for early stage startups.

Tweet

Want to become a guest author on this blog?

9 Comments

  1. 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/

  2. 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.

  3. 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

  4. 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 );

  5. William Knight says:

    9 Jun, 2010

    Brilliant little function hack! Just what I needed…thanks Jeeremie!

  6. 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?

Leave a Reply

Sorry, comments are closed