简体   繁体   中英

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 . On this page I would like to display stock within the given price range: up to $1000, up to $2000 etc etc etc.

functions.php contains:

$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.

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

Sorted using advice from @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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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