I have a certain page in WordPress that have parent pages. I want to exclude those parent pages from WordPress search.
In functions.php I have tried this:
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_parent', '4');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
Well with this code only the post_parent is searchable, but I want the opposite. How would this look like?
UPDATE: Problem solved. Here’s the solution (4 is the ID of the specific page where parent pages is to be excluded from search):
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_parent__not_in', array(4));
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
Kind regards
Johan
With WordPress 3.6, can use new query param:
post_parent__not_in
post_parent__not_in (array) - use post ids. Specify posts whose parent is not in an array.
Use This code function in your Theme’s function.php file with page id which is you want to exclude from your Theme’s custom search bar…And Enjoy with it…!