Exclude pages from WordPress search results

Exclude pages from WordPress search results

Online by JSC0d3
July 21, 2016 | | 124 recognitions

By default, WordPress includes pages in search results. This means that pages such as about, contact, privacy, etc, will be included in search results. These types of pages are not necessary if they are linked in your main navigation menu. WordPress will also include private pages in search results too.

To remove all pages from WordPress search, simple add the snippet below to your theme functions.php file.

function filter_search($query) {
	if ($query->is_search) {
		$query->set('post_type', 'page');
	}
	return $query;
}
add_filter('pre_get_posts', 'filter_search');

 

JSC0d3's Logo
About JSC0d3

JSC0d3 is an entrepreneur, online marketer, and an employee of an IT company. When not building websites, creating content, or helping customers improve their online business, spend time with their wife and two beautiful children. Although he still feels new in WordPress, he enjoys sharing what he has learned with all of you! If you want to get in touch with him, you can do this through this website.

On the same idea

Posted by | April 1, 2019

Images are vital components of every website Before you start questioning the importance of images, just try to imagine your favorite blog or website...

Posted by | March 5, 2019

I’ve been loosely following the noise and #wpdrama surrounding Gutenberg for as long as it has been around and honestly for the most part I’ve...

Posted by | February 24, 2019

To ensure that your site ranks highly in Search Engine Result Pages (SERPs), you’ll need to make it easy for search engine ‘bots’ to explore...

Previous PostBackNext Post

Leave here an impression