Skip to content

Limit WordPress Search Form to only posts

Thanks to Mantas J (PeakMinute) for the following method.

How to limit search to only posts in WordPress without plugin

There are plugins, which can help you to customize this, but I don’t want any more plugins on my WordPress blog. The more plugins you install, the more bloated the system you have and the website will load slower. This may impact user experience, so rather copy and paste a couple lines of code. And yeah, I like fast loading WordPress blog 🙂

I will assume you have a WordPress child theme already.

Copy this code and paste it in the functions.php file

Go to your WordPress dashboard-> Appearance-> Theme File Editor-> functions.php

//show only posts in search results
if (!is_admin()) {
	function wpb_search_filter($query) {
    	if ($query->is_search) {
        	$query->set('post_type', 'post');
    	}
    	return $query;
	}
	add_filter('pre_get_posts','wpb_search_filter');
}

Update the file and now search results should be limited to posts only.

See video here.