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');

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

About Rick

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.
2 comments

Leave a Reply

Add <code> Some Code </code> by using this tags.

*
*