Try these snippets in your functions.php to filter wordpress search
RemoveWordpress pages From Search Results
function cp_remove_pages_from_search() { global $wp_post_types; $wp_post_types['page']->exclude_from_search = true; } add_action('init', 'cp_remove_pages_from_search');
Remove WordPress category From Search Results
add_filter( 'pre_get_posts', 'cp_filter_search' ); function cp_filter_search( $query ) { if ( $query->is_search && !is_admin() ) $query->set( 'cat','-3' ); // 3 is the category id return $query; }
To exclude custom post type
function remove_portfolio_search() { global $wp_post_types; $wp_post_types['dt_portfolio']->exclude_from_search = true; } add_filter('pre_get_posts','remove_portfolio_search');
To exclude some specific post
add_filter( 'pre_get_posts', 'cp_filter_search' ); function cp_filter_search( $query ) { if ( $query->is_search && !is_admin() ) $query->set( 'post__not_in', array(68,11) ); //68,11 are ids of the posts to exclude return $query; }
To exclude custom post type category
add_filter( 'pre_get_posts', 'cp_filter_search' ); function cp_filter_search( $query ) { if ( $query->is_search && !is_admin() ) { //Define the tax_query $taxquery = array( array( 'taxonomy' => 'dt_portfolio_category', // also try lower case, remember a taxonomy name must be in lowercase 'field' => 'slug', 'terms' => array( 'projects-2' ), //slug of the portfolio category 'operator' => 'NOT IN' ) ); $query->set('tax_query', $taxquery ); } }
Limit search to a specific post type
function searchfilter($query) { if ($query->is_search && !is_admin() ) { $query->set('post_type',array('dt_portfolio')); } return $query; } add_filter('pre_get_posts','searchfilter');