samedi 12 septembre 2020

$_POST checking Multiple Checkboxes AJAX

I have a creating a filter (I have select boxes working great), I have checkboxes getting and returning when one checkbox is selected. When multiple checkboxes are checked it does not give me any results. What am I doing wrong?

<?php

add_action('wp_ajax_myfilter', 'my_filter_function'); // wp_ajax_{ACTION HERE}
add_action('wp_ajax_nopriv_myfilter', 'my_filter_function');

function my_filter_function(){

    $args = array(
    'post_type' => 'races_post_type',
    'posts_per_page' => -1
    );

  // Checked ipr
    if( isset( $_POST['ipr'] ) && $_POST['ipr'] == 'ipr' )
        $args['meta_query'][] = array(
            'key' => 'race_type',
      'value' => $_POST['ipr'],
            'compare' => 'LIKE'
        );
  // Checked vr
  if( isset( $_POST['vr'] ) && $_POST['vr'] == 'vr' )
        $args['meta_query'][] = array(
            'key' => 'race_type',
      'value' => $_POST['vr'],
            'compare' => 'LIKE'
        );

    //checked ipr and validate_blog_form()
  if( isset( $_POST['ipr'] ) && $_POST['ipr'] == 'ipr'
  && isset( $_POST['vr'] ) && $_POST['vr'] == 'vr' )
    $args['meta_query'][] = array(
      'key' => 'race_type',
      'value' => $_POST['ipr']['vr'],
      'compare' => 'LIKE'
    );
}

<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">

    <!-- Race State Filter -->
    <?php
        $type_field = get_field_object('field_5f5822bc9a5a1');
        $type_choices = $type_field['choices'];
            foreach( $type_choices as $value => $label ) {
                echo '<label>';
                    echo '<input type="checkbox" name="' . $value . '" value="' . $value . '"/>'; //Only posts with featured images
                echo  $label . '</label>';
                echo '<br>';
        } ?>

    <button>Apply filter</button>
    <input type="hidden" name="action" value="myfilter">

</form>

Let me know if you need to see my request. Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire