From what I understand, you want people to be able to add multiple filters but the code above is only allowing one filter to be used at a time. If this assumption is true, you’ll need to update your code to add to the meta_query
array rather than replace it in each conditional block. For example.
function ransom_filter_function() {
$s = sanitize_text_field( $_POST['s'] );
$iperson = sanitize_text_field( $_POST['iperson'] );
$country = sanitize_text_field( $_POST['country'] );
$state = sanitize_text_field( $_POST['state'] );
$args = array(
'post_type' => 'myposttype',
'posts_per_page' => - 1,
's' => $s,
);
$meta_query = array();
// Inperson Virtual Checkbox
if ( isset( $iperson ) && ! empty( $iperson ) ) {
$meta_query[] = array(
'key' => 'iperson_virtual',
'value' => '"' . $iperson . '"',
'compare' => 'LIKE'
);
}
// Country Dropdown
if ( isset( $country ) && $country ) {
$meta_query[] = array(
'value' => $country,
'compare' => '='
);
}
// State Dropdown
if ( isset( $state ) && $state ) {
$meta_query[] = array(
'value' => $state,
'compare' => '='
);
}
if ( ! empty( $meta_query ) ) {
$args['meta_query'] = $meta_query;
}
$query = new WP_Query( $args );
}
An aside to the above, you should always sanitize user input and then validate the data you have is expected before using it. I’ve used sanitize_text_field()
as an example.