How To Exclude Pages From Search In WordPress

Looking to exclude pages from your WordPress search functionality? All you may need is to add this function your WordPress theme’s fucntions.php file.

Excluding pages from WordPress search

To exclude pages from your WordPress search, you need to add the below function to your WordPress theme’s functions.php file.

  1. Navigate to Appearance > Editor > Theme Functions from your WordPress Dashboard;
  2. Copy and past in the code below;
  3. Click Update File to save the changes.
function exclude_pages_from_search() {
     global $wp_post_types;
     $wp_post_types['page']->exclude_from_search = true;
}

add_action( 'init', 'exclude_pages_from_search' );

Excluding post types from WordPress search

You can use the above solution to also exclude other post types from your WordPress search.

$wp_post_types['your_post_type']->exclude_from_search = true;

Excluding custom post types from WordPress search upon registration

To exclude custom post-types from your WordPress search, you can pass the argument below into when registering your post type.

'exclude_from_search' => true