2017-08-14 106 views
0

我有一個二手車輛網站。查看特定車輛時,有類似車輛的部分。目前,類似車輛是基於相同車身($車型)的車輛。WordPress自定義查詢與2元查詢

我想將其更改爲以相似的價格顯示相同車身風格的車輛。到目前爲止,我已經有了這個代碼的工作:

$vehicletype = get_post_meta($post->ID, 'Vehicle Type', true); 
$id = get_the_ID(); 
$baseprice = get_post_meta($post->ID, 'Retail Price', true); 
$minprice = $baseprice - 10000; 
$maxprice = $baseprice + 10000; 

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$args = array(
    'post_type' => array('vehicle'), 
    'post__not_in' => array($id), 
    'post_status' => 'publish', 
    'order' => 'DESC', 
    'orderby' => 'date', 
    'posts_per_page' => 5, 
    'paged' => $paged, 
    'meta_key' => 'Vehicle Type', 
    'meta_value' => $vehicletype, 
    'meta_key' => 'Retail Price', 
    'meta_value' => $baseprice, 
); 

這明顯顯示車輛與兩個同樣的車身造型和完全相同的價格。我想對此進行更改,以便它不僅顯示完全相同價格的車輛,而且顯示類似價格範圍內(介於$ minprice和$ maxprice之間)的車輛。

我懷疑這會是這個樣子:

'meta_query' => array(
    array(
    'key'  => 'Retail Price', 
    'value' => array($minprice, $maxprice), 
    'type' => 'numeric', 
    'compare' => 'BETWEEN', 
), 
), 

我已經嘗試了幾種不同的方式來得到這個工作,但所有的人都造成「此頁不能正常工作」的錯誤頁面。

任何幫助,將不勝感激。

在此先感謝
威廉

回答

1

嘗試像這樣的是之間不工作:

'meta_query' => array(
    'relation' => 'AND', 
    array 
    (
     'key'  => 'Vehicle Type', 
     'value' => $vehicletype, 
     'compare' => '=' 
    ), 
    array 
    (
     'key'  => 'Retail Price', 
     'value' => $minprice, 
     'type' => 'numeric', 
     'compare' => '>=', 
    ), 
    array 
    (
     'key'  => 'Retail Price', 
     'value' => $maxprice, 
     'type' => 'numeric', 
     'compare' => '<=', 
    ) 
) 
1

試試這個

$vehicletype = get_post_meta($post->ID, 'Vehicle Type', true); 
$id = get_the_ID(); 
$baseprice = get_post_meta($post->ID, 'Retail Price', true); 
$minprice = $baseprice - 10000; 
$maxprice = $baseprice + 10000; 
$query = array(); 
$queryPrice = array(); 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
if("" != $vehicletype) 
    $query[] = ['key'=>'Vehicle Type','value' => $vehicletype, 'compare' => '=']; 

if("" != $baseprice){ 
    $queryPrice[] = ['key'=>'Retail Price','value' => $minprice, 'compare' => '>']; 
    $queryPrice[] = ['key'=>'Retail Price','value' => $maxprice, 'compare' => '<']; 
} 

$args = array(
    'post_type' => 'vehicle', 
    'post__not_in' => array($id), 
     'post_status' => 'publish', 
     'order' => 'DESC', 
     'orderby' => 'date', 
     'posts_per_page' => 5, 
     'paged' => $paged, 
    'meta_query' => array('relation' => 'OR ',$query) 
    'meta_query' => array('relation' => 'AND ',$queryPrice) 
); 
$wp_query = new WP_Query($args); 

此外,查詢有

'meta_key' => 'Vehicle Type', 
'meta_key' => 'Retail Price', 

請確保您所提供的關鍵是什麼是數據庫