I have a working php ajax custom form (using _POST and js). I can’t figure how to filter by checked checkbox items. so if a user checks one/multiple items in the form front end, it would match posts with the same field choices checked. It does work for me if i’m using a select dropdown instead of a checkbox items in the form. My checkbox field is ‘weather’.
Here is my form:
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
<?php
$weather_values = get_field('weather');
if( $weather_values ): ?>
<?php foreach( $weather_values as $weather ): ?>
<input type="checkbox" name="<?php echo $weather; ?>"><?php echo $weather; ?>
<?php endforeach; ?>
<?php endif;
?>
<button>Apply filter</button>
<input type="hidden" name="action" value="myfilter">
</form>
and in the ajax function in the functions.php file, this is the code that does work for me if it was a select dropdown in the form, but right now it always displays all posts, meaning if a checkbox is checked/unchecked it makes no difference.
if( isset( $_POST['weather'] ) )
$args['meta_query'][] = array(
'key' => 'weather',
'value' => $_POST['weather'],
'compare' => 'LIKE'
);
also, I’m wondering how to achieve same thing as if the field was a sub field. tried to wrap it with if(group_field) statement but it didn’t work..