How to Exclude All Pages from WordPress Search Results

5

If you want to know how to exclude all pages from WordPress search results, like we do on wpContent, this tutorial will teach you how. After all, most of the time, readers want to find interesting posts – not pages with boring legal jargon on, and the like.

So, without further ado, let’s begin excluding all pages from WordPress search results!

Initialising the Plugin

To get started, create a new directory in your plugins folder called ‘exclude-pages-from-search’. Create a new file called ‘exclude-pages-from-search.php’ and add the following code to initialise the plugin:

<?php
/*
	Plugin Name: Exclude Pages From Search
	Plugin URI: https://github.com/SamBerson/exclude-pages-from-search
	Description: This plugin excludes all WordPress pages from the site's internal search results.
	Author: Sam Berson
	Version: 1.0
	Author URI: http://www.samberson.com
	License: GNU General Public License v2 or later
	License URI: http://www.gnu.org/licenses/gpl-2.0.html
 */

You’ll now see your new plugin appearing in the Plugins page of the WordPress admin. Go ahead and activate it, though nothing will happen just yet.

Excluding the Pages

Now we’re getting to the good bit! To exclude the pages, simply add this code beneath the plugin’s header info, which we added above.

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

That’s it – now if you do a search for a page on your WordPress site, it won’t appear!

Further Customisations

What’s great is that with this same code snippet, you can simply change the post type to exclude any other kind of post from your search! Also, if you don’t want to make a plugin for this feature, you can simply add the main code to your theme’s ‘functions.php’ file.

In Summary

So, you’ve now learnt how to exclude all pages from WordPress search results. I’m sure you’ll agree that this is a very simple, yet versatile and effective feature to add to your WordPress site. I should also point out, this won’t affect your Google rankings or appearance, and is merely just hiding the pages from the internal search system of your WordPress site.

You can check out this plugin on GitHub or download it from the WordPress plugin repository.

If you have any questions, please leave a comment below, and I’ll be sure to get back to you!


Extra Credit: http://www.wpbeginner.com/wp-tutorials/how-to-exclude-pages-from-wordpress-search-results/

Share.

About Author

Site Admin & Editor

Leave A Reply

  • http://www.worldreviews.org.uk Carole Morris

    Hello Sam. I think you added this feature to my site a while back, but it’s good to see you’ve added it as a plugin now to the WordPress repository. Well done :-)

    • http://www.samberson.com Sam Berson

      Hello Carole – yes you’re right – when I was working on your site, I added this feature so that your about page and contact page didn’t show up in the search. Thanks for reading and commenting! :)

  • Josephine Jackson

    Seems pretty nice and simple, but is there a standalone version so I don’t need to compile the code into a plugin myself? Either on the WP repo or GitHub would be good.

  • Саша Туманов

    This is a kindergarten. What will I do if I have a dozen custom types?