I try to make a sort of fiter for my website. Goal is for the visitors to query results by prices (‘prix’) and hobbies (‘aimerparlapersonne’).
Prices are numbers between min and max. Hobbies is checkbox. I want to allow my visitors to check 2 boxes or more. I made a custom toxonomy of post section.
My problem
Query doesn’t work well with 2 boxes checked. That’s work only with one… My URL result is
?minprice=&maxprice=&aimerparlapersonne=decoration&aimerparlapersonne=sport
Can you help me please ?
This is my form with prices and hobbies
<form action=" <?php $term_link; ?>" method="get">
<label>min:</label>
<input type="number" name="minprice" value="<?php echo $minprice; ?>">
<label>max:</label>
<input type="number" name="maxprice" value="<?php echo $maxprice; ?>">
<label>Hobbies:</label>
<div>
<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>
</div>
<button type="submit" name="">Filter</button>
</form>
I use GET method after that.
<?php
if($_GET['minprice'] && !empty($_GET['minprice']))
{
$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'];
}
?>
And for finish my WP_Query
<?php
$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();?>