Display just child pages of a certain page in search results

StackExchangers!

I have searched the internet left right and center and still cannot find a way to alter the search results that are displayed. Basically I would like no posts and only child pages of this parent page to be displayed in the search results of my WP Site:

Read More

http://universitycompare.com/university-guide/

Otherwise, if none of these pages are displayed it would display null results.

Any help/direction is appreciated, I did try and gather as much information from here, forums and asking webdev friends – But I am completely stuck so I have resorted to asking StackExchange.

Hopefully this isn’t too broad and you understand what I have been trying to figure out for days, otherwise if not, please ask for what you need and I can provide.

Related posts

Leave a Reply

1 comment

  1. Try the following approach.

    Get the page using get_page_by_path()

    http://codex.wordpress.org/Function_Reference/get_page_by_path

    $page = get_page_by_path( 'university-guide' );
    
    $search_query['post_type'] = 'page';
    $search_query['post_parent'] = $page->ID;
    

    The following snippet was taken from Codex page: Creating a search form
    http://codex.wordpress.org/Creating_a_Search_Page

    global $query_string;
    
    $query_args = explode("&", $query_string);
    $search_query = array();
    
    foreach($query_args as $key => $string) {
        $query_split = explode("=", $string);
        $search_query[$query_split[0]] = urldecode($query_split[1]);
    } // foreach
    
    $search = new WP_Query($search_query);
    

    Search results are obtained using WP_Query instance.

    You can use WP_Query codex page to learn more about available options.
    https://codex.wordpress.org/Class_Reference/WP_Query