Search – Avoid custom pages:
- Log in to your WordPress dashboard and navigate to “Appearance” -> “Theme Editor”.
- Click on the “functions.php” file.
- Scroll to the bottom of the file and paste the following code:
function exclude_pages_from_search($query) {
// Ensure this only applies to the main query and is a search query
if ($query->is_main_query() && $query->is_search()) {
// Exclude specific posts by ID
$excluded_ids = array(16,25);
$query->set(‘post__not_in’, $excluded_ids);
}
return $query;
}
add_filter(‘pre_get_posts’, ‘exclude_pages_from_search’);