admin管理员组文章数量:1279021
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();?>
EDIT
I add this code 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 ?
$meta_query = array('passion' => 'OR');
foreach((array) $passion as $passions){
$meta_query[] = array(
'key' => 'aimerparlapersonne',
'value' => $passions,
'compare' => 'LIKE',
);
}
}
$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);
Thanks for your replies solution is here /
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();?>
EDIT
I add this code 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 ?
$meta_query = array('passion' => 'OR');
foreach((array) $passion as $passions){
$meta_query[] = array(
'key' => 'aimerparlapersonne',
'value' => $passions,
'compare' => 'LIKE',
);
}
}
$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);
Thanks for your replies solution is here https://support.advancedcustomfields/forums/topic/wp_query-checkbox-array-string-error/
Share Improve this question edited Nov 21, 2021 at 0:43 Tom J Nowell♦ 61k7 gold badges79 silver badges148 bronze badges asked Nov 11, 2021 at 22:56 frontcodeloverfrontcodelover 1013 bronze badges 2- 3 now with your edit, you have a second question, please close this one, and make that second question in a new one. for those who have the same doubt you had can see the answer. – tiago calado Commented Nov 12, 2021 at 17:45
- I moved your answer to an edit as it was just a link somewhere else, you need to write an answer that isn't just a link offsite, it needs to contain the solution – Tom J Nowell ♦ Commented Nov 21, 2021 at 0:44
1 Answer
Reset to default 0your checkboxes have the same id, the id is unique, otherwise would be a class. in the forms the checkboxes should use each one an unique id and name,check this example at w3schools then when checking for the $_GET we should use the isset because if for some reason does not have that $_GET will return an error in the code.
if(isset($_GET['minprice']) && !empty($_GET['minprice']))
{
$minprice = $_GET['minprice'];
} else {
$minprice = 0;
}
and the !empty($_GET['minprice']) should be set in the html instead as requiered like so
<label>min:</label>
<input type="number" name="minprice" value="<?php echo $minprice; ?>" required >
all this should putt your code working, I even had check nothing more because this should respond to your question, if some issue more appear give feedback.
本文标签: wp queryHow to GET multiple value checkbox WPQuery in Custom ToxonomyCustom Fields
版权声明:本文标题:wp query - How to _GET multiple value checkbox WP_Query in Custom ToxonomyCustom Fields 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741212491a2359400.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论