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:

// custom search filters
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 *