I was recently working on a project that required excluding pages from the built-in WordPress search functionality. After much googling, I came across the small bit of code I was looking for here in the WordPress forum.
Just add the following code to your functions.php file to exclude pages from your WordPress search:
<?php
function mySearchFilter($query) {
if ( ! is_admin() && $query->is_search ) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','mySearchFilter');
?>
Well done sir. That will indeed be helpful and is much cleaner than previous solutions I’ve found.