我试着为我的网站做一件合适的东西。目标是让访问者根据价格(“prix”)和爱好(“AimerParlapersone”)查询结果。
价格是介于最小值和最大值之间的数字。爱好是复选框。我想允许我的访客勾选2个或更多框。我对邮政部门做了自定义分类。
My problem选中两个复选框后,查询无法正常工作。这只是一个。。。我的URL结果是?最小价格=&;最大价格=&;aimerparlapersone=装饰和;aimerparlapersonne=运动
你能帮帮我吗?
这是我的价格和爱好表
<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>
之后我使用GET方法。
<?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\'];
}
?>
和用于完成我的WP\\U查询
<?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
我现在添加了这个代码,这是可行的,但它太严格了。我有3个值为“装饰”、“运动”、“jeux”的复选框。当我检查其中一个时,这就是返回正确的帖子。当我选中2个或更多时,这个返回值只发布值,我希望所有的帖子都只包含1个值,不需要特别更正。
示例Post 1=装饰,sportPost 2=装饰,jeuxPost 3=jeux
如果我检查了装饰返回1号和2号帖子,好的
如果我检查了装饰和jeuxReturn Post 2,但我想要数字2和3。我想这可能是一个比较问题?
$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);
感谢您的回复解决方案就在这里
https://support.advancedcustomfields.com/forums/topic/wp_query-checkbox-array-string-error/