How to Exclude a Page from your WordPress Search Results

, , , ,

If you need to exclude a page or pages from your WordPress search results we simply need to hook into the WP_Query class. WP_Query allows us to customize the way in which our content shown, in this case hidden, on our websites. For more information on this class read the WP_Query documentation.

In this article we will also cover how you can exclude all pages from the search results so that only your posts are searchable. By default, WordPress search will display all your published posts and published pages. Many times, visitors are simply searching for something within your blog rather than on one of your pages. Removing your pages from your WordPress search results may provide your visitors with more relevant results and thus, a better visitor experience.

 

Would you like prompt assistance? If you don’t have time or are not comfortable taking the risk of adding custom code to your website, let us get this done for you now!

 

Exclude Specific Pages from Search

To exclude specific pages from your WordPress search results, add the following code to your functions.php file of the currently active theme or child-theme (preferred).

 

/*
** Hide Specific Page(s) from Search
*/
add_action( 'pre_get_posts', 'endurtech_search_filter' );
function endurtech_search_filter( $query )
{
    if( !$query->is_admin && $query->is_search && $query->is_main_query() )
    {
        $query->set( 'post__not_in', array( 10,11,12 ) );
    }
}

 

Upon implementation, the code snippet above will exclude our pages with ID’s of: 10, 11, and 12 from our search results. If you use this snippet on your website these ID’s would have to change to match the page ID’s to be excluded on your website.

To obtain the ID of your specific page(s), log into your WordPress website backend and click on “Pages“. From there you may hover over any page and take note of the link URL. The number following “post.php?post=” is the number you want. (Yes, post. All content is considered a post, even pages.)

 

Exclude All Pages from Search

To exclude all pages from your WordPress search results, add the following code to your functions.php file of the currently active theme or child-theme (preferred).

 

/*
** Hide All Pages from Search
*/
if( !is_admin() )
{
add_filter( 'pre_get_posts', 'endurtech_search_filter' );
function endurtech_search_filter( $query )
{
if( $query->is_search )
{
$query->set( 'post_type', 'post' );
}
return $query;
}
}

 

The first line of this code snippet checks to make sure that this search is not originating from the WordPress admin backend. It then searches for posts (excluding pages) via the post_type parameter. We could also eliminate posts and only return pages by setting the “post_type” to “pages

 

$query->set( 'post_type', 'pages' );

 

We hope this has helped you learn how to exclude pages from WordPress search results. If this was helpful please take a moment to like us on Facebook, share this on your social media or buy us a cup of coffee.