Tagged: Filter search
Hi, I need limit the search from a ALB search form by category, but I only can filter by posts, pages, etc. There is any wat to limit the search for a category?
Thank you
Any idea?
Thnak you
Hi,
Thank you for your patience, Try adding this code to the end of your child theme functions.php file in Appearance ▸ Editor:
if ( ! function_exists( 'bb_filter_search_results' ) )
{
add_action( 'pre_get_posts', 'bb_filter_search_results' );
function bb_filter_search_results( $query )
{
if ( ! $query->is_admin && $query->is_search )
{
$query->set( 'post_type', array( 'post', 'portfolio', 'page' ) );
$query->set( 'cat', array(43,44) );
}
return $query;
}
}
if ( ! function_exists( 'bb_filter_ajax_search_results' ) )
{
add_filter('avf_ajax_search_query', 'bb_filter_ajax_search_results', 10, 1);
function bb_filter_ajax_search_results( $search_parameters )
{
$defaults = array('numberposts' => 5, 'post_type' => array( 'post', 'portfolio', 'page' ), 'cat' => array(43,44), 'post_status' => 'publish', 'post_password' => '', 'suppress_filters' => false);
$_REQUEST['s'] = apply_filters( 'get_search_query', $_REQUEST['s']);
$search_parameters = array_merge( $defaults, $_REQUEST );
return $search_parameters;
}
}
it will limit the search to an array of categories, in the code look for ‘cat’ => array(43,44) and change the numbers to your categories.
Please note there are two places to make this change.
I tested this with the search element filter Post Types set to All, and with the filter set to Posts
and it only gave me results from the two categories in the code.
Best regards,
Mike