functions.php
/*////////////////////////////////////////////////
//
// Filter search query by custom taxonomy
//
////////////////////////////////////////////////*/
function filtered_search($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
if (isset($_GET['gcats']) && is_array($_GET['gcats'])) {
$tax_query = array(
array(
'taxonomy' => 'guide_category',
'field' => 'term_id',
'terms' => $_GET['gcats'],
)
);
$query->set('tax_query', $tax_query);
}
if (isset($_GET['groles']) && is_array($_GET['groles'])) {
$requested_roles = $_GET['groles'];
$roles_query = array();
foreach($requested_roles as $requested_role) {
if($_GET['s']) {
$role_query = array(
'key' => $requested_role,
'value' => $_GET['s'],
'compare' => 'LIKE',
);
} else {
$role_query = array(
'key' => $requested_role,
'value' => array(''),
'compare' => 'NOT IN',
);
}
$roles_query['relation'] = 'OR';
array_push($roles_query, $role_query);
}
//print_r($roles_query);
$query->set('meta_query', $roles_query);
}
}
}
}
add_action('pre_get_posts','filtered_search');
searchform.php