Has anyone been able to figure this out?
Try this:
function my_search_filter($wp_query) {
if ($wp_query->is_search) {
$wp_query->set('post__not_in', array( -27 ) );
}
return $wp_query;
}
add_filter('pre_get_posts','my_search_filter');
@blueoxp,
Thank you for your reply, however, this doesn’t work either. I could add a category, but I don’t want to add an extra step for the client to have to do. All the blog post will be private only and I don’t want people to be able to view them just because they forgot to select the posts category.
My example doesn’t assume categories or even take them into consideration. 27 is the post or page ID.
post__in is expecting an array of post or page IDs. I think you’re confusing it with in_category.
No, I understood what your function did and it made perfect sense. My original findings similar to this topic were based on categories. and I originally tried to modify the if statement to work with page exclusion instead of category exclusion. For example:
// search filter
function my_search_filter($wp_query) {
if ($wp_query->is_search) {
$wp_query->set('cat_id','27');
}
return $wp_query;
}
add_filter('pre_get_posts','my_search_filter');
This function actually works for excluding categories in the search.
I guess I didn’t really explain it very well. Your code works properly.
Here is a better explanation of what I am trying to do (I hope). I have a members only page for my website. This page is actually set to display all my “blog” posts. It will not contain any static information. Basically, I don’t want any post to display in the search.
What would be the best way of going about this? Thanks again for your comments!
Ah, try this where 27 is your blog category ID
// search filter
function my_search_filter($wp_query) {
if ($wp_query->is_search) {
$wp_query->set( 'category__not_in', '27' );
}
return $wp_query;
}
add_filter('pre_get_posts','my_search_filter');
I ended up doing the following:
// search filter
function my_search_filter($query) {
if ($query->is_search) {
$query->set('post__in',array(3,5,7,17,9,11,13));
}
return $query;
}
add_filter('pre_get_posts','my_search_filter');
I am not sure if this is the most efficient way of doing it, but I think this will suite my needs. The search is only pulling from the pages that I want searched.
I really appreciate your help blueoxp. Your suggestions have been most helpful!
So it’s been awhile since you posted here, but here’s a solution I found today. In my specific case, I wanted to ignore any text that the user entered into the page that’s set as the blog posts page.
function mySearchFilter($query) {
if ($query->is_search) {
$excludeId = get_option('page_for_posts');
$query->set('post__not_in', array($excludeId));
}
return $query;
}
add_filter('pre_get_posts','mySearchFilter');
Todd
(@toddbeyond5280com)
I needed to exclude one page from the results that is a modal and should only be viewed that way. I modified the code from Tim and thought I would put it here in case anyone else just has 1 page that needed to be excluded. 460 is the page id that needed to be excluded, so just replace that with the id of the page you want to exclude
function mySearchFilter($query) {
if ($query->is_search) {
$excludeId = 460;
$query->set('post__not_in', array($excludeId));
}
return $query;
}
add_filter('pre_get_posts','mySearchFilter');