Custom Programming – Modifications

Search – Avoid custom pages:

  1. Log in to your WordPress dashboard and navigate to “Appearance” -> “Theme Editor”.
  2. Click on the “functions.php” file.
  3. 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’);