Today I want to share with you a quick and easy tutorial on how to exclude pages from WordPress search results.
By default, WordPress will include your site’s pages in its search results, which can sometimes be annoying and quite frankly irrelevant to your user’s needs. In order to combat this, we can add simple function into our functions.php (or custom_functions.php for Thesis users) file in order to filter out those pages in our search results.
The Function to Exclude Pages from WordPress Search Results
Simply copy and paste this function into your functions.php or custom_functions.php file:
This function does exactly what we need it to do. By placing a filter on the pre_get_posts hook, we can determine what we want to show in our search results. In this case, all we want to show is content from our blog posts, so we simply define that variable (and only that variable) for the query.
And there you have it! Simply save your file and test out a search query. You have excluded pages from your WordPress search results!
Hope you enjoyed the post, and don’t forget to subscribe to these tips and tutorials in the section below! Also, check out the follow up post on how to include custom post types in WordPress search results!
