How To Exclude Specific Pages From Your WordPress Search Results

How To Exclude Specific Pages From Your WordPress Search Results
Today we are going to use a trick to exclude pages in WordPress search, since by default WP does not have that functionality.

We can do it in two ways, today we are going to talk about the first, for which we will need more knowledge, by code.

As we have said before, WordPress by default does not bring this functionality, which will show us all the results in the search.

We are going to obtain pages, posts, or products, and here there may be a problem, since if we have an online store, we do not want anything to appear that is not a product.

We may also want to hide certain types of posts, or posts made by certain authors, or categories.

Users will appreciate a cleaner search, and it gives a much better image of our site.

Exclude Specific Pages From WordPress Search

We are going to carry out this action through code, and here we are going to take into account two very important things.

The first, obviously to use code we are going to need some previous knowledge, since we are going to touch files that can ruin our site.

The second and most important, we are going to add code, which means that we will need to create a child theme.

We need this child theme because, if we write on WordPress’ own files, when we receive an update, everything we have changed will be overwritten, it will disappear.

Exclude Specific Category

That said, we are going to start with the first code, in which we are going to exclude Categories.

We will need the ID of the category that we want to exclude from the search, once we have it, in the functions.php file of our child theme we add the following code.

//Exclude Single Category ID From WordPress Search
function vpsb_search_filter( $query ) {
    if ( $query->is_search && !is_admin() )
        $query->set( 'cat','-1' );
    return $query;
}
add_filter( 'pre_get_posts', 'vpsb_search_filter' );

In this example, ‘-1’ is the ID of the category we want to exclude.

If we want to exclude more than one category, we can include the IDs separated by commas.

//Exclude Multiple Category IDs From WordPress Search
function vpsb_search_filter( $query ) {
    if ( $query->is_search && !is_admin() )
        $query->set( 'cat','-1, -2, -4' );
    return $query;
}
add_filter( 'pre_get_posts', 'vpsb_search_filter' );

Exclude Specific Tags From WordPress Search

Now we are going to exclude archived posts under a specific tag, using the following code.

//Exclude Single Specific Tag ID From WordPress Search
if ( $query->is_search && !is_admin() )
        $query->set( 'tag','-1' );
    return $query;
}
add_filter( 'pre_get_posts', 'vpsb_search_filter' );

If we want to exclude more than one tag, we can include the IDs separated by commas.

//Exclude Multiple Tag IDs From WordPress Search
if ( $query->is_search && !is_admin() )
        $query->set( 'tag','-1, -2, -4' );
    return $query;
}
add_filter( 'pre_get_posts', 'vpsb_search_filter' );

Excluding Specific Terms in a Custom Taxonomy From WordPress Search

We added the following code to remove a term from the search in a custom taxonomy from WordPress search results.

//Exclude Custom Taxonomy From WordPress Search
function vpsb_modify_search_query( $query ) {
    global $wp_the_query;
    if( $query === $wp_the_query && $query->is_search() ) {
        $tax_query = array(
            array(
                'taxonomy' => 'term',
                'field' => 'slug',
                'terms' => 'action',
                'operator' => 'NOT IN',
            )
        );
        $query->set( 'tax_query', $tax_query );
    }
}
add_action( 'pre_get_posts', 'vpsb_modify_search_query' );

We can modify the array to exclude the results we want. The taxonomy array is the custom taxonomy.

The array of terms is the taxonomy category to be excluded. With the taxonomy category, we can also exclude several taxonomy categories by changing the matrix to “terms” => “rock”, “alternative”, “record”.

In this way we eliminate all the publications that are not part of the rock category.

In the case that we want to show posts that are only in the rock category, we will change the code from NOT IN to IN.

Exclude Single Author From WordPress Search

We can use the following code to exclude an author’s posts from WordPress search results.

//Exclude Single Author From WordPress Search
function wpb_search_filter( $query ) {
    if ( $query->is_search && !is_admin() )
        $query->set( 'author','-1' );
    return $query;
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );

We can also exclude multiple authors at the same time directly with the following code.

//Exclude Multiple Authors From WordPress Search
function wpb_search_filter( $query ) {
    if ( $query->is_search && !is_admin() )
        $query->set( 'author','-24, -12, -19' );
    return $query;
}
add_filter( 'pre_get_posts', 'wpb_search_filter' );

If you have not created your website yet, you may be interested Create Your Website With WordPress.

References:
vpsbasics.com
wpbeginner.com

1 reply on “How To Exclude Specific Pages From Your WordPress Search Results”

  • This is what I was looking for. I wanted to exclude 1 category from the search results. There is no such feature inside wordpress itself. Now I know how to deal with it. It looks like adding the code to the file – will it disappear when updating the theme? Will you need to add it every time you update your theme files?

Leave a Reply

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

Pin It on Pinterest

Shares
Share This