How to Exclude Pages from WordPress Search Results
Excluding specific pages or posts from WordPress search results can be useful in various scenarios, such as when you have content that you don’t want to appear in search results. To exclude pages from WordPress search results.
If you prefer not to use a plugin and are comfortable with editing code, you can achieve the same result by adding some custom code to your theme’s functions.php file. Here’s how:
- Access Your Theme’s Functions.php:
- Go to your WordPress admin dashboard.
- Navigate to “Appearance” > “Theme Editor.”
- Find and click on the “Theme Functions” (functions.php) file on the right-hand side.
2. Add Custom Code: Add the following code to your theme’s functions.php file to exclude specific pages from search results:
/**
* Excluding pages from search result.
*/
function exclude_pages_from_search() {
global $wp_post_types;
$wp_post_types['page']->exclude_from_search = true;
}
add_action( 'init', 'exclude_pages_from_search' );
3. Save Changes:After adding the code, click the “Update File” button to save your changes.
Remember that using custom code requires some technical knowledge, and you should always back up your site before making any changes to your theme files. Using a plugin is generally a safer and more user-friendly option for most WordPress users.