Exclude certain pages from WordPress search results

For various reasons, we may need to exclude certain pages from WordPress search results. This isn’t the same as excluding all pages from the search results. We only want to exclude specific pages. Here’s one way to do that. Add this code to your child theme’s functions.php, or better yet make it a plugin. If you don’t know how to make a custom plugin, you can do that easily with Pluginception.

Don’t want to manage this manually, or not comfortable adding custom code to your site? Check out Search Control for WordPress. It makes this much easier for you!

function jp_search_filter( $query ) {
	if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
		$query->set( 'post__not_in', array( 10,11,20,105 ) );
add_action( 'pre_get_posts', 'jp_search_filter' );

In the example above, we’re excluding pages with IDs 10, 11, 20, and 105 from the search results. You will need to change these numbers to match the IDs of the pages you want to exclude.

The WP_Query class in WordPress is a powerful tool we have at our disposal for customizing the content shown on our sites. Check out the WP_Query documentation to see all the things it can do.

16 thoughts on “Exclude certain pages from WordPress search results

  1. Shraddhananda Moharana says:


    Thanks a lot for the code, actually my requirement was to exclude some post as well as categories so i had mentioned the following ways. Mentioned below is the code for your reference.

    function aabsys_search_filter( $query ) {
      if ( $query->is_search && $query->is_main_query() ) {
        $query->set( 'post__not_in', array( 2,55 ) ); // post ids to exclude from search
        $query->set( 'category__not_in' , array( 1,5 ) ); // category ids to exclude from search
    add_filter( 'pre_get_posts', 'aabsys_search_filter' );
    • The is_main_query function ensures the query you’re targeting is the “main” one on the page, and not some custom secondary query. It should work on your site with this in place unless you have some other code tinkering with queries.

  2. Ryan says:

    It works great :) thanks so much …

    I just wanted to know if there is some way to hide the author information for posts/pages in wordpress search results.

    PS could I add it to the same plugin?


    • Ryan, hiding that information would require some customizations to your theme, preferably in a child theme. The theme controls how the search results are displayed. This code just controls the search visibility of the specified pages.

  3. dt.k says:

    The problem with this approach is that it will also hide posts/pages from the Admin panel search. This isn’t very helpful if you have a lot of posts/pages on your site and you need to search for a specific post/page which you have omitted from appearing in the search results.

    To get round this, add the following to the conditional statement:


    So the end result would be:

    function jp_search_filter( $query ) {
    if ( !$query->is_admin && $query->is_search && $query->is_main_query() ) {
    $query->set( ‘post__not_in’, array( 10,11,20,105 ) );
    add_action( ‘pre_get_posts’, ‘jp_search_filter’ );

  4. Andy Globe says:

    Thanks. This worked for me. I wanted to exclude gallery appears in search results and this code does exactly what I need. Thanks

Leave a Reply