Exclude WordPress pages from Search results

Here's a cool snippet to remove the WordPress Pages from search results.

Pretty straight forward, paste the following code inside your functions.php file:

/*-----------------------------------------------------------------------------------*/
/* Exclude pages from Search Results */
/*-----------------------------------------------------------------------------------*/

function exclude_pages_from_search() {
    global $wp_post_types;
    $wp_post_types['page']->exclude_from_search = true;
}
add_action('init', 'exclude_pages_from_search');Code language: PHP (php)

You can also filter the WordPress Search by custom post type, if you need to 😉

Comments

  1. Hi.. Great code.. but do you know a way of only Exclude or Include page that have a type of metadata value. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *