[英]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.