Extend search to specific custom post types

How can I extend or limit search to specific custom post type in WordPress?

To add your custom filters to the search function, add this code to your plugin:

<?php
// custom search filters
add_filter('pre_get_posts','wpster_searchfilter');
function wpster_searchfilter($query) {
	if ($query->is_search && !is_admin() ) {
 		// set global search to post and projects (custom post type)
 		$query->set('post_type', array('post','projects'));
 		// exclude specific post ID
 		$query->set('post__not_in', array(747,749,751)); 	
 		// return max 50 records
 		$query->set( 'posts_per_page', '50' );
	}
	return $query;
}
?>

Submit a Comment

Your email address will not be published. Required fields are marked *