WordPress Search Filter Only for Page with Child of Child of Child of Child of Child

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');

Leave a Comment