How to Search Specific Posts, Pages or Custom post in WordPress

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 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 page 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.

Inspire us with your love!

FacebookTwitterReddit
Saiful Islam

Saiful Islam, the founder of aThemeArt, is a successful freelancer, a father of two boys, and an enthusiast in coding, YouTube, and gaming. Connect with me on Twitter, Facebook, and Instagram to stay updated with my latest endeavors.

You can check also