How to Exclude Pages from WordPress Search Results

  • Post author:
  • Post category:Wordpress

To exclude specific pages from WordPress search results, you can use a custom code snippet. This code will modify the search query to exclude the pages you specify. Here’s how you can do it:

// Exclude specific pages from search results
function exclude_pages_from_search_results($query) {
    if (is_search()) {
        $exclude_ids = array(1, 2, 3); // Replace these numbers with the IDs of the pages you want to exclude
        $query->set('post__not_in', $exclude_ids);
    return $query;
add_filter('pre_get_posts', 'exclude_pages_from_search_results');

In the code above: Replace 1, 2, 3, etc., with the actual page IDs you want to exclude from search results.