Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
add_filter('pre_get_posts','SearchFilter');

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

["child_of"]=>
    int(104)

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

Why?

Thanks!

share|improve this question
add comment

1 Answer

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
add comment

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.