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, you can add simple function into our functions.php
file in order to filter out those pages in our search results.
Key Takeaways
- Pages show up in WordPress search results by default.
- In this step-by-step tutorial, you are going to create a function that completely removes pages from your WordPress 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 you need it to do. By modifying the main WordPress query via the pre_get_posts
hook, you can determine what you want to show in your search results. In this case, all you want to show is content from your blog posts, not your 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!
Clarus Dignus
Works great. Thanks for this function and for presenting it concisely.
Thomas Griffin
You’re quite welcome! Now those pages will stay away from your WordPress search results!
FD
Thanks Thomas. Very useful. Saved my day.
Thomas Griffin
You are quite welcome! Glad you found it helpful to exclude pages from your search results. 🙂
BT
It works. Thank you. Thank you. Thank you. 😊
Sayan Dutta
Works great. Thanks
chad
How do you make wordpress show both pages and posts in the search?
Thomas Griffin
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.Alexandra
It excludes posts as well.
Thomas Griffin
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!
Alice William
Any idea how to exclude one particular page from search?
Thomas Griffin
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.Mozi
This code is better than the others available on almost all sites.
Thomas Griffin
Glad you approve Mozi!
Joey Gavilo
Hi Thomas,
It excludes products too, How can I include products?
Thanks
Thomas Griffin
Hey Joey – depending on the name of the post type, you would just add
'products'
to the post type array.Earl
Hi Thomas, how to exclude posts of a certain category or an category array from the search results? Thank you!
Thomas Griffin
Hey Earl,
Great question! You would do it like this:
$query->set( 'cat', '-1, -2' );
Where
1
and2
are the category IDs and the-
sign denotes to make sure they are excluded from the query.Hope that helps!