Home › Forums › Front-end Issues › WP_Query checkbox array string error
Hello,
I try to build a form for my visitor to allow “filter” post. I have 2 fields with price and 1 with checkbox.
Now that’s work but it’s too restrictiv. I have 3 checkbox with value “decoration”, “sport”, “jeux”. When I checked one it’s that’s return the right post. When I check 2 or more this return only post the values I want all the post with only 1 value correct not specially too.
Exemple
Post 1 = decoration, sport
Post 2 = decoration, jeux
Post 3 = jeux
If I checked decoration
Return Post 1 and 2 ok
If I checked decoration and jeux
Return Post 2 but I want numbers 2 and 3. I think it’s maybe a compare problem ?
My code
<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="passion[]" value="decoration">Décoration</p>
<p><input type="checkbox" id="aimerparlapersonne" name="passion[]" value="sport">Sport</p>
<p><input type="checkbox" id="aimerparlapersonne" name="passion[]" value="jeux">Jeux</p>
</div>
<button type="submit" name="">Filter</button>
</form>
<?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['passion'] && !empty($_GET['passion']))
{
$passion = $_GET['passion'];
// Get the selected options
$meta_query = array('passion' => 'OR');
foreach((array) $passion as $passions){
$meta_query[] = array(
'key' => 'aimerparlapersonne',
'value' => $passions,
'compare' => 'LIKE',
);
}
}
?>
<?php
$qobjet = get_queried_object();
$args = array(
'post_type' => 'post'
'posts_per_page' => 20 ,
'tax_query' => array(
array(
'taxonomy' => $qobjet->taxonomy,
'field' => 'id',
'terms' => $qobjet->term_id,
),
),
'relation' => 'AND',
'meta_query' => array(
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
),
'meta_query' => $meta_query,
),
);
$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();?>
By the way a
var_dump($_GET['passion']);
Give me this
0 => string 'decoration' (length=10)
1 => string 'sport' (length=5)
Hope I will have some help cause I really need this form 🙂
Your problem is here, you are trying to set the “meta_query” argument twice.
'meta_query' => array(
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
),
'meta_query' => $meta_query,
In this case “meta_query” will always be set to $meta_query and the part using “prix” will never happen. Your meta query also has other issues
<?php
$meta_query = array(
'relation' => 'AND'
);
if($_GET['minprice'] && !empty($_GET['minprice'])) {
$minprice = $_GET['minprice'];
} else {
$minprice = 0;
}
if($_GET['maxprice'] && !empty($_GET['maxprice'])) {
$maxprice = $_GET['maxprice'];
} else {
$maxprice = 999999;
}
$meta_query[] = array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
);
if($_GET['passion'] && !empty($_GET['passion'])) {
$passions = $_GET['passion'];
$nested_query = array(
'relation' => 'OR'
);
foreach((array) $passion as $passions){
$nested_query[] = array(
'key' => 'aimerparlapersonne',
'value' => $passions,
'compare' => 'LIKE',
);
}
$meta_query[] = $nested_query;
}
$args = array(
'post_type' => 'post',
'posts_per_page' => 20,
'tax_query' => array(
array(
'taxonomy' => $qobjet->taxonomy,
'field' => 'id',
'terms' => $qobjet->term_id,
),
),
'meta_query' => $meta_query
);
I accidentally added an s here
$passions = $_GET['passion'];
this should have been
$passion = $_GET['passion'];
from your original code
@John
I have another question. How to make to “checked” when I do a filter ?
Now When I filter my post checkbox are still “uncheck”.
Hope U will read this.
<input type="checkbox" id="aimerparlapersonne" name="passion[]" value="decoration" <?php
if (!empty($_GET['passion']) && in_array('decoration', $_GET['passion'])) {
echo 'checked="checked";
}
?>>Décoration
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.