By default, WordPress Search feature displays revealed posts and revealed pages in search results. usually when users are searching for something in a web log, it’s most likely a post rather than a page. If you wish to hide pages from showing when your readers use the default WordPress search box on your web site, you’ve got several choices.

In this post, we are going to show you how to create your search a lot of relevant and fewer crowded by excluding pages from WordPress search results.

Using Code Snippets

First, you add the following code to your a site-specific plugin or theme’s functions.php file. This filter function returns search results for posts only and excludes all pages from displaying on your wordpress sites search results once your readers use your search box.



add_filter( 'pre_get_posts', 'ata_exclude_pages' );
/**
 * This filter function modifies the main WordPress query to remove pages from search results.
 *
 * @param object $query  The original query
 * @return object $query The amended query.
 */
function ata_exclude_pages( $query ) {
	
    if ( $query->is_search && !is_admin() ) {
		$query->set( 'post_type', 'post' );
    }
    
    return $query;
    
}

This code removes all posts from displaying in your search results and only returns results for pages.

So let’s see that we have two Custom Post Types, “product” and “download”. We want to exclude the product , download post type from search results.



add_filter( 'pre_get_posts', 'ata_exclude_pages' );
/**
 * This filter function modifies the main WordPress query to remove pages,posts etc from search results.
 *
 * @param object $query  The original query
 * @return object $query The amended query.
 */
function ata_exclude_pages( $query ) {
	
    if ( $query->is_search && !is_admin() ) {
		$query->set('post_type', array('product', 'download'));
    }
    
    return $query;
    
}

Often the code will not return any value. Make sure you, Put these when you define your custom post type .


'query_var' => true,
'exclude_from_search' => false,

If you wish to exclude only specific pages , posts id from the search query, past the following code into your theme’s functions.php file:



add_filter( 'pre_get_posts', 'ata_exclude_pages' );
/**
 * This filter function modifies the main WordPress query to remove pages from search results.
 *
 * @param object $query  The original query
 * @return object $query The amended query.
 */
function ata_exclude_pages( $query ) {
	
    if ( $query->is_search && !is_admin() ) {
		$query->set( 'post__not_in', array( 148,125,30,36 ) );
    }
    
    return $query;
    
}

In the example on top of, we’re excluding pages with IDs 148, 125, 30 and 36 from the search results. you’ll need to modification these numbers to match the IDs of the pages you wish to exclude.

Exclude Search Using Plugin

With this plugin’s you can exclude any page, post or whatever from the WordPress search results by checking off the corresponding checkbox on post/page edit page.

We hope this article helped you learn the way to exclude pages from WordPress search results. If you discover this article useful, don’t forgot to join our newsletter for more informative articles and follow us on Facebook and Twitter

Published by Editorial Staff

aThemeArt Editorial Staff prepares and cooks all the content that are published on athemeart.com. They are responsible for all type of web content including blog, social posts, videos, documentation etc.

Leave a Reply

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

twenty − two =

This site uses Akismet to reduce spam. Learn how your comment data is processed.