Exclude pages from WordPress search result page

Add this to your child themes functions file using a code editor like Notepad++.

You will need to change the page I.D’s in the code to your own.

Exclude Specific Pages From Search Results

add_filter( 'pre_get_posts', 'exclude_pages_search_when_logged_in' );
function exclude_pages_search_when_logged_in($query) {
    if ( $query->is_search && is_user_logged_in() )
        $query->set( 'post__not_in', array( 1, 2, 3, 4, 5 ) ); 

    return $query;
}

Exclude All Pages From Search Results

add_action('pre_get_posts','exclude_all_pages_search');
function exclude_all_pages_search($query) {
    if (
        ! is_admin()
        && $query->is_main_query()
        && $query->is_search
        && is_user_logged_in()
    )
        $query->set( 'post_type', 'post' );
}

Source http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Exclude_Pages_from_Search_Results

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)