No longer want a certain page or a group of pages to appear in WordPress search results? This may confuse those who haven’t found our guide deliberately. After all, you want search engines to index your pages and visitors to easily find as much engaging content as possible. However, there are exceptions. For instance, you may want to post a URL on social media but don’t feel like employing membership-only features. Since it cannot be searched, this will let you measure the engagement and reach. eCommerce websites also need to hide a few pages, like checkout, account panel, or “thank you!”. Regardless, here’s how to exclude pages from search results on WordPress.
1. Use a WordPress plugin to exclude one or all pages from search results
We rarely begin our guides by telling you to install a WordPress plugin. However, sometimes that’s the safest option, especially for beginners. Advanced users may also find it unnecessary to write long lines of code if they need to exclude many pages or custom fields. With that said, there are some free WordPress search plugins on the repository. However, those are mainly designed for quick, short, and simple exclusions. Though we aren’t affiliated in any way, we suspect you’ll stumble upon and opt for the top WordPress plugin for search results, SearchWP, eventually.
Therefore, we’ll cut to the chase and use an all-encompassing method. It essentially generates a dedicated search engine for your website, which, you’d agree, is powerful. We’ll use it for a demonstration to help beginners and experienced webmasters alike. Plus, it has a high degree of customization, as you can remove all pages or only specific posts or pages, even custom post types. Follow these instructions to exclude a WordPress page or pages from search results via SearchWP:
- Head over to the SearchWP website and click Get SearchWP.
- Sign up, pay, and head to the “Downloads” section of your account.
- Click the Download SearchWP button.
- Complete the installation of a WordPress plugin via FTP.
- Visit your WordPress Admin section. Go to Settings → SearchWP.
- Switch to the License tab and enter the license key you got after a purchase. Select Activate.
- The way you proceed depends on your goals:
1. Stop all pages from appearing in search results
This is a quick and easy solution to eradicate all pages from search results. We already mentioned eCommerce website owners as people that desire this option. Though you may think these are already removed, theme designers leave the option open in case business owners want to blog or post product reviews. With that said, removing all pages from WordPress search results works through the Engines tab in the “SearchWP” section. We’ll delve into it in greater detail below. Luckily, since you don’t want any pages to appear, you can bypass all this doing the following:
- Select Remove next to “Pages” or click the Sources & Settings button.
- Under “Edit Settings (Default engine)”, remove a checkmark in front of “Pages”.
- Click the Done button.
- Click the Save Engines button in the top right corner.
2. Remove a page from search results
First, we need to mention the “Engines” tab again. You could see posts, pages, and media files as sources of search results, which are default search engine values. You can choose to edit any by clicking the down arrow or selecting Remove, then Add New. Pages and posts will have an “Applicable Attribute Relevance” with sliders that decide the “weight” each part of a page will have.
For example, if you pull the slider to the left for “Title”, the page won’t appear when visitors search for variations of heading 1. It will still appear if contents appear in the slug, content, or except, however. This is unreliable unless you stop taxonomies from appearing, especially for bloggers. Therefore, we propose a faster and more suitable solution. You have two options:
- Go back to the SearchWP website and revisit the “Downloads” section. Find the “Exclude UI extension” page and click the Download Extension button.
- Visit the Exclude UI download portal directly before selecting Download Extension.
The procedure is straightforward from here:
- Install the extension identically to the plugin itself.
- In the WordPress Admin, go to Pages → All Pages.
- Open the page you want to exclude.
- In the main menu on the right-hand side of WordPress Block Editor, make sure you’re on the “Page” tab.
- Put a checkmark in front of Exclude from search.
- Click the Update/Publish button.
2. Add code snippets to remove pages from WordPress search results
Though we don’t suggest newcomers mess with code snippets in this particular case, the option still exists. Make sure to create a backup of your site, regardless of which of the three options you go with:
- Edit the functions.php file by accessing your website via FTP (Go to “root/wp-content/themes/child-theme-name/”)
- Use a site-specific WordPress plugin.
- Install a dedicated plugin for adding code snippets.
With that done, here are several examples of commonly requested results users have employed successfully. Credit goes to the respective authors. Also, while they should all work, we provided multiple choices to give you an alternative in case they don’t, or you have a preference. To start, to remove all pages from search results, utilize one of these three code snippets:
add_action('pre_get_posts','exclude_all_pages_search');
function exclude_all_pages_search($query) {
if (
! is_admin()
&& $query->is_main_query()
&& $query->is_search
&& is_user_logged_in()
)
$query->set( 'post_type', 'post' );
}
if (!is_admin()) {
function wpt_search_filter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','wpt_search_filter');
}
function remove_pages_from_search() {
global $wp_post_types;
$wp_post_types['page']->exclude_from_search = true;
}
add_action('init', 'remove_pages_from_search');
The following code snippet with stop specific pages from appearing on a front-end search (numbers represent page IDs):
add_filter( 'pre_get_posts', 'exclude_pages_search_when_logged_in' );
function exclude_pages_search_when_logged_in($query) {
if ( $query->is_search && is_user_logged_in() )
$query->set( 'post__not_in', array( 11, 22, 33, 44, 55 ) );
return $query;
}
You can also exclude all posts an author wrote, where “-18” is the author ID/user ID.
function wp_search_filter( $query ) {
if ( $query->is_search && !is_admin() )
$query->set( 'author','-18' );
return $query;
}
add_filter( 'pre_get_posts', 'wp_search_filter' );
If there are pages from multiple users you don’t want to appear, use this snippet:
function wp_search_filter( $query ) {
if ( $query->is_search && !is_admin() )
$query->set( 'author','-18, -13, -15' );
return $query;
}
add_filter( 'pre_get_posts', 'wp_search_filter' );