Filter Search In WordPress

We can filter our search in WordPress according to posts or custom post.

Like I have two custom posts type videos and news and I want WordPress search page will display records out of only these 2 custom posts whenever user search record.

So to achieve this we have to write this code in our function.php.

function filter_search($query) {
if ($query->is_search) {
$query->set(‘post_type’, array(‘videos’, ‘news’));
};
return $query;
};
add_filter(‘pre_get_posts’, ‘filter_search’);

Leave a Reply