[英]How to query posts using GET with numeric values within a given range?
我希望能够给我的访问者以选择在所选价格范围内的特定产品的选项。
<select name="retail_price" class="form-control">
<option disabled selected value="">Price up to</option>
<?php for ($i=1;$i<=45;$i++): ?>
<option value="<?= $i*1000 ?>">to $<?= number_format($i*1000,0,'',' ') ?></option>
<?php endfor; ?>
</select>
例如,如果他们选择“最高$ 1 000”并单击“搜索”按钮,则会被带到https://example.com/?retail_price=1000 。 在此页面上,我想显示给定价格范围内的股票:最高$ 1000,最高$ 2000等,等等。
functions.php
包含:
$metaQuery = [];
(...)
if ( isset( $_GET['retail_price'] ) ) {
$metaQuery[] = [
'key' => 'retail_price',
'value' => $_GET['retail_price'],
'compare' => '='
];
}
$query->set('meta_query', $metaQuery);
有什么建议吗?
编辑 :使用来自@Sky的建议进行排序。
if ( isset( $_GET['retail_price'] ) ) {
$metaQuery[] = [
'key' => 'retail_price',
'value' => array( '0', $_GET['retail_price'] ),
'type' => 'numeric',
'compare' => 'BETWEEN',
];
使用@Sky的建议进行排序。
if ( isset( $_GET['retail_price'] ) ) {
$metaQuery[] = [
'key' => 'retail_price',
'value' => array( '0', $_GET['retail_price'] ),
'type' => 'numeric',
'compare' => 'BETWEEN',
];
做到了。 谢谢大家的帮助 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.