WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

after browsing question from this site I found the workaround for this:

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

When I do this, its only search child of parent 21 not child of child of parent 21.

Also, query_vars only accept single value for post_parent.

any solution for this is highly appreciated.

thanks

share|improve this question

You can probably do something like this. It's probably less efficient, but should find all descendants of a given page rather than just direct children.

function find_descendants($post_id) {
    $descendant_ids = array();
    $pages = get_pages("child_of=$post_id");
    foreach ($pages as $page) { array_push($descendant_ids, $page->ID); }
    return $descendant_ids;
}

function SearchFilter($query) {

    if ($query->is_search) {
        $query->set ( 'post__in', find_descendants(21) );
    }
    return $query;
}
add_filter('pre_get_posts','SearchFilter');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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