How to Exclude Pages, Posts, Authors, Category, Tags in WordPress

How to Exclude Pages, Posts, Authors, Category, Tags in WordPress

WordPress by default, will display all posts, pages and custom post types in its search results. This is generally fine for most but there maybe times when you want to exclude certain pages from appearing, such as, running an e-commerce website. If you used WordPress search it could show your visitors the checkout page or my account page or even a thank you page. Similarly, if you had a membership only website there would be specific pages or custom post types that you would want to exclude from WordPress search or you could simply want more control of your WordPress search results to exclude specific tags, categories or even hide posts from a specific author.

By optimising your WordPress search results to exclude unnecessary results you will offer a better user experience and improve the overall usability of your website.

In this guide we will show you how to exclude pages, posts, authors, category and tags from WordPress search results using your functions.php file.

IMPORTANT In this tutorial, we'll be directly editing WordPress theme files and we recommend that you create a child theme of your existing parent theme. By using a child theme you will be able to apply modifications without altering the parent theme files and ensuring any changes you make are kept following any parent theme updates.
TIP If you don't feel comfortable with editing the functions.php file directly, we would recommend you use the Code Snippets plugin. This plugin will enable you to easily add, manage and delete WordPress code snippets from your dashboard. To find out more about adding custom code snippets using a plugin see our tutorial How to Add Custom Code Snippets to WordPress with the Code Snippets Plugin for more information.

If you don’t feel comfortable with editing the functions.php file or using the Code Snippets plugin to exclude specific posts or pages from appearing in WordPress search results page, you can use the Search Exclude plugin to achieve a similar result.

Exclude Specific Pages From WordPress Search

To exclude specific pages from WordPress search simply copy and paste the following code into your functions.php file:

//Exclude Certain Pages from WordPress Search 
function vpsb_search_filter( $query ) {
  if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
    $query->set( 'post__not_in', array( 10,11,20,105 ) );
  }
}
add_action( 'pre_get_posts', 'vpsb_search_filter' );
TIP Don’t forget to replace the ‘post__not_in’, array( 10,11,20,105 ) with the page ID you wish to exclude, for example, if the page IDs were 5, 10, 15, you would change the code to $query->set( ‘post__not_in’, array( 5,10,15 ) ); .

Exclude All Pages From WordPress Search

To exclude all pages from WordPress search simply copy and paste the following code into your functions.php file:

//Exclude All Pages from WordPress Search
if (!is_admin()) {
function vpsb_search_filter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','vpsb_search_filter');
}

Now when a search term is entered into WordPress, the search will display the results for posts only and will exclude all pages from being displayed.

Exclude All Posts From WordPress Search

To exclude all pages from WordPress search simply copy and paste the following code into your functions.php file:

//Exclude All Posts from WordPress Search
if (!is_admin()) {
function vpsb_search_filter($query) {
if ($query->is_search) {
$query->set('post_type', 'page');
}
return $query;
}
add_filter('pre_get_posts','vpsb_search_filter');
}

Now when a search term is entered into WordPress, the search will display the results for pages only and will exclude all posts from being displayed.

Exclude All Except Specific Custom Post Types From WordPress Search

To exclude all post types and only show specific custom post types in your WordPress search simply copy and paste the following code into your functions.php file:

// Exclude All Except Specific Custom Post Types From WordPress Search
function vpsb_search_filter( $query ) {
    if ( !$query->is_admin && $query->is_search && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'recipe', 'cuisine', 'product' ) );
    }
}
add_action( 'pre_get_posts', 'vpsb_search_filter' );
TIP Don’t forget to replace the ‘post_type’, array( ‘post’, ‘recipe’, ‘cuisine’, ‘product’ with the custom post types you wish to include, for example, if the custom post types were was media, photo, video, you would change the code to $query->set( ‘post_type’, array( ‘media’, ‘photo’, ‘video’ ) );.

Exclude Single Author From WordPress Search

To exclude a single author from WordPress search simply copy and paste the following code into your functions.php file:

//Exclude Single Author from WordPress Search
function vpsb_search_filter( $query ) {
    if ( $query->is_search && !is_admin() )
        $query->set( 'author','-12' );
    return $query;
}
add_filter( 'pre_get_posts', 'vpsb_search_filter' );
TIP Don’t forget to replace the ‘author’,’-12′ with the author number you wish to exclude, for example, if the author number was 5, you would change the code to  $query->set( ‘author’,’-5′ );.

Exclude Multiple Authors From WordPress Search

To exclude a multiple authors from WordPress search simply copy and paste the following code into your functions.php file:

//Exclude Multiple Authors from WordPress Search
function vpsb_search_filter( $query ) {
    if ( $query->is_search && !is_admin() )
        $query->set( 'author','-12, -22, -26' );
    return $query;
}
add_filter( 'pre_get_posts', 'vpsb_search_filter' );
TIP Don’t forget to replace the ‘author’,’-12, -22, -26′ with the author numbers you wish to exclude, for example, if the author numbers were 5, 10, 15, you would change the code to  $query->set( ‘author’,’-5, -10, -15′ );.

Exclude Single Category From WordPress Search

Before entering the code into the WordPress functions.php file, you will need to find the Category ID that you wish to exclude from WordPress search.

To exclude a single category ID from WordPress search simply copy and paste the following code into your functions.php file:

//Exclude Single Category ID from WordPress Search
function vpsb_search_filter( $query ) {
    if ( $query->is_search && !is_admin() )
        $query->set( 'cat','-5' );
    return $query;
}
add_filter( 'pre_get_posts', 'vpsb_search_filter' );
TIP Don’t forget to replace the ‘cat’,’-5′ with the category number you wish to exclude, for example, if the category number was 10, you would change the code to  $query->set( ‘cat’,’-10′ );.

Exclude Multiple Categories From WordPress Search

To exclude a multiple category IDs from WordPress search simply copy and paste the following code into your functions.php file:

//Exclude Multiple Category IDs from WordPress Search
function vpsb_search_filter( $query ) {
    if ( $query->is_search && !is_admin() )
        $query->set( 'cat','-7, -10, -21' );
    return $query;
}
add_filter( 'pre_get_posts', 'vpsb_search_filter' );
TIP Don’t forget to replace the ‘cat’,’-7, -10, -21′ with the category IDs you wish to exclude, for example, if the category IDs were 5, 10, 15, you would change the code to  $query->set( ‘cat’,’-5, -10, -15′ );.

Exclude Single Tag From WordPress Search

To exclude a single tag ID from WordPress search simply copy and paste the following code into your functions.php file:

//Exclude Single Specific Tag ID from WordPress Search
if ( $query->is_search && !is_admin() )
        $query->set( 'tag','-15' );
    return $query;
}
add_filter( 'pre_get_posts', 'vpsb_search_filter' );
TIP Don’t forget to replace the ‘tag’,’-15′ with the tag ID you wish to exclude, for example, if the tag ID was 5, you would change the code to  $query->set( ‘tag’,’-5′ );.

Exclude Multiple Tag IDs From WordPress Search

To exclude a multiple tags IDs from WordPress search simply copy and paste the following code into your functions.php file:

//Exclude Multiple Tag IDs from WordPress Search
if ( $query->is_search && !is_admin() )
        $query->set( 'tag','-15, -22, -44' );
    return $query;
}
add_filter( 'pre_get_posts', 'vpsb_search_filter' );
TIP Don’t forget to replace the ‘tag’,’-15, -22, -44′ with the tag IDs you wish to exclude, for example, if the tag IDs were 5, 10, 15, you would change the code to  $query->set( ‘tag’,’-5, -10, -15′ );.

Exclude Custom Taxonomy From WordPress Search

To exclude a term in a custom taxonomy from WordPress search simply copy and paste the following code into your functions.php file:

//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' );
TIP Don’t forget to replace the custom taxonomy term ‘taxonomy’ => ‘term’,
with term you wish to exclude, for example, if the term was birds, you would change the code to  ‘taxonomy’ => ‘birds’, .

You can change the code array to anything you like. The taxonomy array is the custom taxonomy. The terms array is the taxonomy category to exclude. With the taxonomy category you can also exclude multiple taxonomy categories by changing the array to ‘terms’ => ‘action’,’adventure’,’movie’. The operator array will show posts that aren’t part of the action category. If you want to show posts that are only in the action category you would change the code from NOT IN to IN.

That’s it. You have now successfully updated your functions.php file to exclude Pages, Posts, Authors, Categories or Tags from being displayed in your WordPress search results.

VPSBasics

VPSBasics

This guide was written by the VPS Basics editorial team, led by Gilberto Van Roosen. They are a unique blend of people who are dedicated to providing highly detailed, comprehensive and easy to follow tutorials, written in plain English. They specialise in tutorials for managing Linux servers, its software and WordPress.

Leave a Reply

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

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