• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
SideGains Logo

SideGains

Blogging Tips & Tutorials

  • Blog
    • Blogging
    • SEO
    • WordPress Tips
    • Tutorials
    • SideGains
    • Motivation
    • Content
    • Social Media
    • Making Money
    • Search Engines
    • Domains & Hosting
    • PPC
    • Frugal Living
  • About
  • Blogging Resources
  • Contact
Home » WordPress Tips » How to Exclude Pages from WordPress Search Results Without Plugins

How to Exclude Pages from WordPress Search Results Without Plugins

Last Updated on 12th March 2021 by Paul Leave a Comment

I’m documenting a fix to a problem I’ve fixed on the SideGains blog today. It relates to the native WordPress search function that pulls both pages and posts into the results page… I want to exclude pages from WordPress search results to show only posts.

Why do I want to do this?

Since my pages are accessible from my navigation menu, I feel it’s unhelpful to have them returned in a search. I only want search results to retrieve the blog posts most relevant to my what my visitors are looking for.

For me, having pages in search results just muddies the waters.

I also don’t want to add a plugin to do this for me. Read the following posts to understand why:

  • Pros and Cons of WordPress Plugins
  • Can You Have too Many WordPress plugins?

So in this post, I’m going to explain how to exclude pages from WordPress search results. This ensures the search function only returns relevant blog posts as results.


This tutorial requires you to add several lines to your function.php file. This is an important file and so you need to be careful whenever you edit it. If you break the file it can bring down your WordPress blog.

Be sure to use a WordPress child theme and make copy of your functions.php file before you change it. If something goes wrong, you’ll be able to restore the working copy to fix it.


Editing Your functions.php File to to Exclude Pages from WordPress Search Results

The following code should work for any WordPress version and theme. I use the Genesis Framework for the SideGains blog, though I have tested this on other themes.

  1. The first step is to locate your function.php file and make a backup copy.
  2. Go to the end of the functions.php file and add the following code:
/** Exclude Pages from WordPress Search Results Without a Plugin **/
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');
}

3. Save the file.

That’s it!

Now try running a search on your WordPress blog and check to see that no pages display in the results.

An Explanation of How This Function Excludes Pages from WordPress Searches

The first “if statement” checks whether the search has been made in the admin panel, and if it has, it ignores the function. This is important because we don’t want to exclude pages from searches we make within your WordPress admin panel… we only want the function to affect searches made by your visitors.

Assuming someone used the search tool on your blog (and not within the admin panel), the function forces the WordPress search function to return only posts in the results. This is achieved by requesting the ‘post’ within the ‘post_type’ parameter.

As an aside, if you only wanted your WordPress search function to return pages only in the search results, you just need to set the ‘post_type’ parameter to ‘page’ instead of ‘post’.


Summary

There are lots of reasons why you’d want to include or exclude pages from WordPress search results. For me it was a question of only returning my blog posts that match a visitor search. In my view, it’s posts rather than pages that my visitors will want to see.

Of course, you might have a different reason or need, and as a consequence you may want to exclude posts from searches and only return pages.

I hope this short tutorial helps you exclude pages from your WordPress search results. Of course if you feel uncomfortable editing your functions.php file you could always search WordPress for a plugin to do the job for you.

That’s all for now.

Paul


Excluding WordPress Pages from Search Results

Has this helped you to exclude pages from your WordPress searches? Perhaps you have another way to achieve this? Please feel free to ask a question or drop me a comment below.

Filed Under: WordPress Tips

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Disclosure: This page contains affiliate links from which I may make a commission at no cost to you. See how I make money for more information.

Paul Franklin - SideGains

My name’s Paul.

I’ve been using WordPress to build blogs since 2005. SideGains condenses my 15+ years of experience of blogging, SEO and PPC in one place… I hope it helps YOU!

Find out more about SideGains, what makes me tick & why I started this blog.

SiteGround Banner
WP Engine Banner
SEO PowerSuite - all-in-one SEO tool



  • Blog Post Formatting Guide
  • Blogging Goals Planner
  • Google Analytics Dashboard Sheet

Footer

About SideGains

SideGains is designed to give you the blogging tips & tools you need to create an online income outside of the 9 to 5.

Find out more about SideGains, what makes me tick & why I began the SideGains project.

Information

  • How SideGains Makes Money
  • Privacy
  • Contact Me
  • SideGains HTML Sitemap

Work With Me

  • Hire Me!
  • Guest Writers

Copyright © 2023 SideGains.com

  • Facebook
  • Twitter
  • Pinterest
  • Instagram