Have you ever wondered how you may narrow down your search results to only include certain article types?
It isn’t difficult. By editing the functions.php file, we’ve already shown you how to disable the search option in WordPress. Now we’ll do the same thing, except this time we’ll filter our search results.
Add the following code to your functions.php file:
1
2
3
4
5
6
7
8
9
10
|
function searchfilter( $query ) { if ( $query ->is_search && !is_admin() ) { $query ->set( 'post_type' , array ( 'post' , 'page' )); } return $query ; } add_filter( 'pre_get_posts' , 'searchfilter' ); |
Take note of the line that says:
1
|
$query ->set( 'post_type' , array ( 'post' , 'page' )); |
By modifying the values in the array variable, you can filter the search results. It’s now set to show posts and pages, but you may change it to show anything you like.