Specific Post Types in Search Results



As I have already showed you how to disable the search feature in WordPress and how to exclude pages from WordPress search results by modifying the functions.php file of theme. Now In this tutorial I will display specific post types in search results of WordPress by modifying in functions.php file.

Just open your functions.php file of active theme and add the following codes:

function mysearchfilter($query) {

    if ($query->is_search && !is_admin() ) {
        $query->set('post_type',array('video', 'testimonial'));
    }

    return $query;
}

add_filter('pre_get_posts','mysearchfilter');

You can add any post types in the array variable like I have added video and testimonial.

Hope this will help you.