How To Limit Search Results For Specific Post Types in WordPress
Have you ever tried to limit your search results to specific post types? Its not very hard.
We can modify the search feature in WordPress by modifying the functions.php file.
This is very easy with using this default hook.
pre_get_posts
function searchfilter($query) { if ($query->is_search && !is_admin() ) { $query->set('post_type',array('post','page')); } return $query; } add_filter('pre_get_posts','searchfilter');
See the line that says
$query->set('post_type',array('post','page'));
By modifying this you can easily modify particular post type for search. simply add or remove particular post type in array variables.
Post Views: 12