Close Panel
 
 

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:

// 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 all! Replace the posts category IDs (‘12,14,16,17,18′) by your owns and it should work.

Technorati Tags: , ,

Popularity: 7% [?]

 

Related posts

| Subscribe to Feed | Email the author

6 Responses to “Wordpress – Exclude pages from search results”

  1. 1
    jason allen Says:

    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?

  2. 2
    jeeremie Says:

    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. 3
    jasprit Says:

    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. 4
    Wouter de Winter Says:

    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. 5
    Jeeremie Says:

    Thanks Wouter for the tip.

  6. 6
    Como excluir as páginas do resultado de busca ? | Dicas WordPress | Tudo Para WordPress Says:

    [...] Dica retirada do site Web-Kreation [...]

 

Leave a Reply

Wrap all code in <pre lang="LANGUAGE" colla="-">...</pre> and replace 'LANGUAGE' by 'html4strict' for HTML, 'php', 'javascript', 'css'...