How To Limit Search To Only Posts In WordPress Without Plugin

If you want to know how to limit search to only posts in WordPress without plugin this guide will show you exactly that. 

Why do you need to limit search to only posts?

When you just install wordpress on your hosting and add a search box anywhere on the website it will show all posts and pages. So where is the problem?- You might want to exclude pages, which visitors can see and limit to only post. 

For example: 

  • You can have a separate page delivering your lead magnet after people subscribe to your newsletter. 
  • You may have a thank you page (or even several), which got tracking set up,- it may ruin your analytics.
  • You may have a demo page or sales page you haven’t finished.
  • Upcoming or past event pages, which are not relevant or even confusing for people.
  • You may have a mini course which you don’t want anyone to find so easy.

Maybe it’s just me, but even those pages like privacy policy, terms and conditions and similar should not appear in search results and I’d rather show blog posts only. Leave those necessary legal pages in the menu or in the footer.

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;

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

YouTube video
About Mantas J

Blogger, marketer, coffee lover,- currently now developing several online projects. I know a thing or two about digital marketing :) Follow me on Twitter or YouTube.