jeudi 11 novembre 2021

How to _GET multiple value checkbox WP_Query

I try to do a WQ_query with a form and multiple checkbox. That's doesn't work... The goal is to filter my post by price and checkbox. I don't know why this is don't work well...

Result is ?minprice=&maxprice=&aimerparlapersonne=decoration&aimerparlapersonne=sport

How to fix this ?

Form <p><input type="checkbox" id="aimerparlapersonne" name="aimerparlapersonne" value="decoration">Décoration</p> <p><input type="checkbox" id="aimerparlapersonne" name="aimerparlapersonne" value="sport">Sport</p>

Get

                 {
                     $minprice = $_GET['minprice'];
                 } else {
                     $minprice = 0;
                 }

                 if($_GET['maxprice'] && !empty($_GET['maxprice']))
                 {
                     $maxprice = $_GET['maxprice'];
                 } else {
                     $maxprice = 999999;
                 }


                    if($_GET['aimerparlapersonne'] && !empty($_GET['aimerparlapersonne']))
                    {
                        $aimerparlapersonne = $_GET['aimerparlapersonne'];



                    }`


WP_query

                $qobjet = get_queried_object();
$args = array(
                                     'post_type' => 'post',
                                     'posts_per_page' => 20 ,
                                     'meta_query' => array(
                                             array(
                                                     'key' => 'prix',
                                                     'type' => 'NUMERIC',
                                                     'value' => array($minprice, $maxprice),
                                                     'compare' => 'BETWEEN'
                                             ),
                                             array(
                                                    'key' => 'aimerparlapersonne',
                                                    'value' => $aimerparlapersonne,
                                                    'compare' => 'LIKE'
                                            ),

                                                'relation' => 'AND',
                                                'tax_query' => array(

                     array(
                                 'taxonomy' => $qobjet->taxonomy,
                                 'field' => 'id',
                                 'terms' => $qobjet->term_id,
                     ),

                 ),
             ),      );


                             $query = new WP_Query($args);

             ?>

                <?php if ($query->have_posts() ) : while ($query->have_posts() ) : $query-> the_post(); // run the loop ?>
                <?php get_template_part( 'content-category', get_post_format() ); ?>
                <?php endwhile; ?>
            <?php endif;?>
    <?php wp_reset_query();?>```





Aucun commentaire:

Enregistrer un commentaire