I'm trying to limit search to a parent page and its own child pages.

I've changed searchform.php adding this code:

<input type="hidden" value="<?php
    global $post;
    $ancestors2 = $post->ancestors;
    if ($ancestors2) {
        echo end($ancestors2);
    } else {
        echo $post->ID;
    } ?>" name="childof" />

Also I've add a function to my theme's functions.php:

function SearchFilter($query) {
if (is_search() && !empty($_GET['s']) && !empty($_GET['childof'])) {
    $parent_page_id = intval($_GET['childof']);
    $query->set('child_of', $parent_page_id );
return $query;

I've tested variables with var_dump($query) and it returned:


But, I still can see search results from other pages and not only from parent page (or its child pages).




WP_Query does not have child_of argument.

You might be looking for post_parent or post_parent__in, see Post & Page Parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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