How To Exclude Pages From WordPress Search Results (Certain Pages)

Sometimes you may need to exclude pages from WordPress search results. Or if you want to exclude only certain pages. Here we share a solution to archived that. Because WordPress Search default displays all published posts and pages in search results.

As we know that WordPress is very popular for blogging, so it is most important to show posts in search results rather than pages. Sometimes a single word is found in your pages and these pages will show in a search result. But they are irrelevant and annoying to visitors. That is the reason we need to prevent to show pages on the search result.

However, in this article, we will discuss how to avoid to show all pages or certain pages in search results. Thus, WordPress search results make more relevant to your search content. So, let’s move ahead and start learning about it.

Exclude All Pages In WordPress Search Results

Add these codes in your themes function.php file. It will exclude the pages from WordPress search. Basically, this code uses the pre_get_posts hook to filter search results. First check that the search is not performed by Admin user. Then we set the post_type to post. And If search performed by admin then prevent to hide posts/pages from the admin panel.

//Exclude all pages from WordPress Search
function ss_search_filter( $query ) {
    if ( !$query->is_admin && $query->is_search && $query->is_main_query() ) {
        $query->set( 'post_type', 'post' );
    }
}
add_action( 'pre_get_posts', 'ss_search_filter' );

Above code removes all pages from displaying in your search results and only returns results for posts.

Note: If this is your first time to adding code snippets in WordPress, then we suggest you use Code Snippets Plugin.

However, if you want to display only pages in a search results then you can update above code from $query->set( 'post_type', 'post' ); to $query->set( 'post_type', 'page' );.

Exclude Only Specific Posts and Pages In WordPress Search Results

If you want to exclude only specific posts and pages then you need to add these code in your function.php file.

//Exclude specific posts and pages from WordPress Search
function ss_search_filter( $query ) {
    if ( !$query->is_admin && $query->is_search && $query->is_main_query() ) {
        $query->set( 'post__not_in', array( 2, 9, 17 ) );
    }
}
add_action( 'pre_get_posts', 'ss_search_filter' );

In the above example, we are excluding pages with IDs 2, 9, and 7 from the search results.

Add Specific Custom Post Types in WordPress Search Results

If you need to show only specific post types (custom post type) into the search result, then you have to add these code in your function.php file.

// Show only specific post types into WordPress Search
function ss_search_filter( $query ) {
    if ( !$query->is_admin && $query->is_search && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'recipe', 'cuisine', 'product' ) );
    }
}
add_action( 'pre_get_posts', 'ss_search_filter' );

Above filter will show only the post types that we specified into the post_type array, rest are excluded in search query.

Reference: The WP_Query class in WordPress is a great tool to customize the content to display in our site. You can check WP_Query documentation to explore all properties, methods, parameters, and many more.

We hope you have found this article helpful. Let us know your questions or feedback if any through the comment section in below. You can subscribe to our newsletter and get notified when we publish new WordPress articles. Moreover, you can explore WordPress related other interesting articles.


Join the Discussion.