简体   繁体   English

如何使用GET在给定范围内的数值查询帖子?

[英]How to query posts using GET with numeric values within a given range?

I want to be able to give my visitors and option to choose specific products within the selected price 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>

Eg if they select "up to $1 000" and click "Search" button they will be taken to https://example.com/?retail_price=1000 . 例如,如果他们选择“最高$ 1 000”并单击“搜索”按钮,则会被带到https://example.com/?retail_price=1000 On this page I would like to display stock within the given price range: up to $1000, up to $2000 etc etc etc. 在此页面上,我想显示给定价格范围内的股票:最高$ 1000,最高$ 2000等,等等。

functions.php contains: functions.php包含:

$metaQuery = [];
        (...)

    if ( isset( $_GET['retail_price'] ) ) {

        $metaQuery[] = [
        'key'     => 'retail_price',
        'value'   => $_GET['retail_price'],
        'compare' => '='
        ];

        }


$query->set('meta_query', $metaQuery);

Any advice? 有什么建议吗?

Edit : Sorted using avice from @Sky. 编辑 :使用来自@Sky的建议进行排序。

        if ( isset( $_GET['retail_price'] ) ) {
        $metaQuery[] = [
        'key'     => 'retail_price',
        'value'   => array( '0', $_GET['retail_price'] ),
        'type'    => 'numeric',
        'compare' => 'BETWEEN',
        ];

Sorted using advice from @Sky. 使用@Sky的建议进行排序。


        if ( isset( $_GET['retail_price'] ) ) {
        $metaQuery[] = [
        'key'     => 'retail_price',
        'value'   => array( '0', $_GET['retail_price'] ),
        'type'    => 'numeric',
        'compare' => 'BETWEEN',
        ];

Did the trick. 做到了。 Thank you all for your help :) 谢谢大家的帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM