0

I'm trying to have my search results show only results that are a child of the 'news' category or a child of the 'work' page (ID = 16).

This is what I have so far:

    function SearchFilter($query) {
        if ($query->is_search) {
            $query->set('category_name', 'news');
            $query->set('post_parent', '16');            

        }
        return $query;
    }

    add_filter('pre_get_posts','SearchFilter'); 

The problem is it only searches for posts that belong to 'news' AND have a parent page ID of '16', which is obviously no posts.

So how do better refine search? I'd prefer not to use a plugin, that's why i am here.

Thanks, Drew

1

The only way I could figure it out was to build a list of ID's to exclude like this:

/*
* Hide some pages from search results
*/
function SearchFilter($query) {
    if ($query->is_search) {

        $exclude = '18,20,12,22,132,76,81,78,84,91';
        // Get all children of #18
        $pages = get_pages('post_parent=18');
            foreach($pages as $child) {            
            $exclude .=  "," . $child->ID;
        }

        $query->set('post__not_in', explode(",", $exclude));                        
    }
    return $query;
}

add_filter('pre_get_posts','SearchFilter'); 

You could use any of the WP_Query parameters to do similar things (like a build a list of ID's to include).

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.