0

Anyone know of an easy way to alter the default WordPress search from searching ALL pages, to searching only child pages from a specific page parent?

I know of the filter you can use to alter whether pages or posts are searched:

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

Maybe there is a way to add further information here to specify a particular page parent?

2 Answers 2

2

I have no idea if this will work, but couldn't you reference the post_parent in the query args?

function SearchFilter($query) {
if ($query->is_search) {
    //$query->set('post_type', 'page');
    global $post;
    $query->set( 'post_parent', $post->ID );
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');

Again, completely untested. I'm not terribly familiar with using $query->set().

0

Chip's answer works fine for me. (Sorry, I have like zero reputation so cannot vote up or comment...)

I'm actually using it for displaying only search results on a specific template:

function SearchFilter($query) {
    if ($query->is_search) {
        //$query->set('post_type', 'page');
        $query->set( 'meta_key', '_wp_page_template' );
        $query->set( 'meta_value', 'template-content.php' );        
    }
    return $query;
}
add_filter('pre_get_posts','SearchFilter');
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.