Comments

That’s spot on. I’ve been searching for a way to exclude pages all day and this is by far the most straight forward and reliable method I’ve found. Now I just need to figure out how to exclude selected posts from search results 🙂

@Derek: thanks for mentioning that. I’ll look into it and see if I can figure out what’s going on.

@James: Thanks! There’s a way to exclude certain posts, but it depends on how you’re selecting them. You can use tags on the posts you want to ignore and the set the search filter above to ignore those posts. I plan on writing a post on it at some point in the near future. If you’re in a rush, I can email it to you. Just let me know!

I really should check my email before responding to comments… I’ll send a copy of the script to your email!

Actually, it wouldn’t. The RSS feed is a separate query from the Search query. When filtering search queries, we’re doing it on the Search Query like so:

if ($query->is_search) {
    $query->set('tag__not_in', $exclude_tags_array);
    $query->set('post_type', 'post');
}

When filtering the RSS feed, we’re doing it on Feed Query like so:

if ($query->is_feed) {
    $query->set('cat','-1');
}

(The snippet above excludes any posts in the “Uncategorized” category from appearing in your RSS/Atom Feeds.)

I haven’t worked too much with the Feeds, but I would presume it’s similar to filtering the Search query. I’ll look into it at some point in the near future.

Worked a treat, thanks! Seems like this should be a default option in the admin to exclude pages.

@Derek – I’m on 2.8.2 and it is working just fine. Dunno!

Greetings, thank you for that solution. However I’ve encountered problems. I constantly receive this warning (pretty much everywhere – posting a comment, saving a page in admin, etc.):
Warning: Cannot modify header information - headers already sent by (output started at /www/wp-content/themes/default/functions.php:1) in /www/cms/wordpress/wp-includes/pluggable.php on line 865
I am on 2.8.2. Any ideas?

@Andrey: Make sure there are no empty lines in your functions.php file, that will throw the error message you’re getting. Blank lines within a bock of code are fine, but blank lines between blocks of code are not.

Thanks for this, I have it set up to search through pages only.

What I’d like to do though is search the children pages of the parent page with an ID of 10. Is this possible?

Thanks.

@ITM Design: I believe it’s possible. If what you want to do is only search pages with a parent ID of ’10’, then I think you could set it as follows:

if ($query->is_search) {
    $query->set('post_parent', '10');
}

Again, I’m not 100% possible this will work, but try it and let us know.

The WP search function searches the Posts table to display posts and pages that have the keywords a person is searching for. You can set the search filter to only search for posts/pages with a specific tag or you can set it to search posts only in a specific category, but I don’t believe you can set it to only search the tag and category table. Why would you want that? Those tables only hold tag slugs and category slugs, no other data. Are you trying to search only posts that have specific tags or posts that are in a specific category?

To exclude only certain pages with specific names? For instance, exclude the “Contact” page, but include the “About” page? You can set that using the following query variable.

[post__not_in] => Array()

@ Terje: Do you mean exclude a post category? If so, yes, you can do the following:

$query->set('cat', '-1');

Where “-1” is the id# of the category you want to exclude. I’m not sure how to exclude pages with a specific page template though. You could try an array of the page ids though, like so:

$do_not_include = array('1,2,3');
$query->set('post__not_in', $do_not_inlcude);

All together it would look like this:

function mySearchFilter($query) {
    
    $do_not_include = array('1,2,3');

    if ($query->is_search) {
        $query->set('cat', '-1');
        $query->set('post__not_in', $do_not_include);
    }
    return $query;
}

add_filter('pre_get_posts','mySearchFilter');

This works like a charm. Thanks so much. One issue, though. Is there any way to use this snippet but still include pages in the results when searching via the WordPress admin/backend?

I don’t want end-users being able to search pages but from within the wp admin I would still need to be able to search across both posts and pages. Is there any simple tweak to your suggested code that would accomplish this?

Hey db, When I’m in the WP dashboard and click ‘edit pages’ and try using the search box at the top right to find a certain page to edit, pages with those keywords don’t show up in the results.

@David: Try this:

< ?php
function mySearchFilter($query) {

    if ( $query->is_search && !is_admin() ) {
        $query->set('post_type', 'post');
    }
    return $query;
}

add_filter('pre_get_posts','mySearchFilter');
?>

@David: You’re welcome!

What this edit does is check to see if we’re doing a Search query and if we’re not in the Admin section of the site.

Nice code, and definitely something I could use!

However I’m struggling to leave out posts within a custom post_type. In this case the custom post_type is ‘advisors’. The example beneath does NOT work:

function mySearchFilter($query) {
if ($query->is_search) {
	$query->set('post_type', '-advisors');
}
return $query;
}
add_filter('pre_get_posts','mySearchFilter');

Any help will be appreciated!
Arnoud

Hey thanks it helped me a lot …………… But there is one problem i could not solve. I want to exclude certain pages with given parent id ie i do not want to include pages with parent id lets say 10………. Can anyone help me……… Thanks

@Arnoud: the minus sign (-) only works with numbers, not words. I’ve read that with WP 3.0, custom post types can be excluded from all default queries, but I have played with it enough to comment intelligently.

@Newbie: See this comment: #95

How can I search tags in posts and then exclude posts from search results with a certain tag in them?

Comments are closed.