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. For most websites, this is not necessary or desired. In order to change this behavior, we can add simple function into our functions.php
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
file of your WordPress theme:
/**
* This function modifies the main WordPress query to remove
* pages from search results.
*
* @param object $query The main WordPress query.
*/
function tg_exclude_pages_from_search_results( $query ) {
if ( $query->is_main_query() && $query->is_search() && ! is_admin() ) {
$query->set( 'post_type', array( 'post' ) );
}
}
add_action( 'pre_get_posts', 'tg_exclude_pages_from_search_results' );
This function does exactly what we need it to do. By modifying the main WordPress query via 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, not our pages.
Want a WordPress website that’s secure AND fast? My friends at WP Engine are offering 3 months free on all annual plans. Click here to claim your special WP Engine offer!
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 get more great WordPress content! Also, check out the follow up post on how to include custom post types in WordPress search results!
Works great. Thanks for this function and for presenting it concisely.
You’re quite welcome! Now those pages will stay away from your WordPress search results!
Thanks Thomas. Very useful. Saved my day.
You are quite welcome! Glad you found it helpful to exclude pages from your search results. 🙂
It works. Thank you. Thank you. Thank you. 😊
Works great. Thanks
How do you make wordpress show both pages and posts in the search?
Hey Chad – this is how WordPress search works by default. If you want to keep pages in search along with other custom post types, you can just include
'page'
as part of the array when you customize the post types.It excludes posts as well.
Make sure you’ve copied it as stated and that no other plugins are interfering with the ability to exclude pages from the search results. It should work as anticipated!
Any idea how to exclude one particular page from search?
Just underneath the other query modification, you would add something like this:
$query->set( 'post__not_in', array( 10 ) );
Change out
10
for the ID of the page you want to exclude from your search results.This code is better than the others available on almost all sites.
Glad you approve Mozi!
Hi Thomas,
It excludes products too, How can I include products?
Thanks
Hey Joey – depending on the name of the post type, you would just add
'products'
to the post type array.