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

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).



share|improve this question
up vote 0 down vote accepted

WP_Query does not have child_of argument.

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

share|improve this answer

Your Answer


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.