Filter search results by post type

WordPress Tuttorial Filter search results by post type – Selecting one of these filters will display search results exclusively from that filter. Adding this PHP code to the functions.php of your wordpress theme will only search posts, and custom post types, excluding pages.

 

 

function search_posts_filter( $query ){
if ($query->is_search){
$query->set('post_type',array('post','custom_post_type1', 'custom_post_type2'));
}
return $query;
}
add_filter('pre_get_posts','search_posts_filter');

Be Sociable, Share!

Leave a comment