2015-07-13 80 views
1

我一直在構建自定義搜索。搜索功能有多個下拉列表,用戶可以留空,或從多個選項中進行選擇。這些數據保存在'wine-note'自定義帖子類型的帖子的自定義字段中。使用元查詢進行自定義搜索

到目前爲止它工作有點。我的搜索結果頁面看起來像這樣:

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
query_posts(array(
'post_type' => 'wine-note', 
'posts_per_page' => '10', 
'paged' => $paged, 
'meta_query' => array(
    array(
     'key' => 'vintage', 
     'value' => $vintage_select, 
     'compare' => 'LIKE' 
     ), 
    array(
     'key' => 'full_list_regions', 
     'value' => $region_select, 
     'compare' => 'LIKE' 
     ), 
    array(
     'key' => 'producer_name', 
     'value' => $winery_select, 
     'compare' => 'LIKE' 
     ), 
    array(
     'key' => 'S100', 
     'value' => $score_100_from, 
     'compare' => '>=' 
     ), 
    array(
     'key' => 'S100', 
     'value' => $score_100_to, 
     'compare' => '<=' 
     ), 
    array(
     'key' => 'S20', 
     'value' => $score_20_from, 
     'compare' => '>=' 
     ), 
    array(
     'key' => 'S20', 
     'value' => $score_20_to, 
     'compare' => '<=' 
     ), 
    array(
     'key' => 'wine_rating', 
     'value' => $rating_from_select, 
     'compare' => '>=' 
     ), 
    array(
     'key' => 'wine_rating', 
     'value' => $rating_to_select, 
     'compare' => '<=' 
     ) 
     ) 
     ));?> 

的問題,這是搜索排除不具有特定的自定義字段,即使該下拉選擇留空任何職務。

即時通訊 - 只顯示包含所有自定義字段(數據與否)的帖子。

因此,我修改了一下我的代碼,添加了一些if語句,以便只在用戶從下拉列表中選擇一個選項時才搜索該自定義字段。 我現在的搜索結果代碼如下所示:

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
query_posts($args = array(
'post_type' => 'wine-note', 
'posts_per_page' => '10', 
'paged' => $paged, 
'meta_query' => array(

     ) 
     ) 

if ($vintage_select) { 
$args['metaquery'][] = array(
    'key' => 'vintage', 
    'value' => $vintage_select, 
    'compare' => 'LIKE' 
); 
} 

if ($region_select) { 
$args['metaquery'][] = array(
    'key' => 'full_list_regions', 
    'value' => $region_select, 
    'compare' => 'LIKE' 
); 
} 

if ($winery_select) { 
$args['metaquery'][] = array(
    'key' => 'producer_name', 
    'value' => $winery_select, 
    'compare' => 'LIKE' 
); 
} 

if ($score_100_from) { 
$args['metaquery'][] = array(
    'key' => 'S100', 
    'value' => $score_100_from, 
    'compare' => '>=' 
); 
} 

if ($score_100_to) { 
$args['metaquery'][] = array(
    'key' => 'S100', 
    'value' => $score_100_to, 
    'compare' => '<=' 
); 
} 

if ($score_20_from) { 
$args['metaquery'][] = array(
    'key' => 'S20', 
    'value' => $score_20_from, 
    'compare' => '>=' 
); 
} 

if ($score_20_to) { 
$args['metaquery'][] = array(
    'key' => 'S20', 
    'value' => $score_20_to, 
    'compare' => '<=' 
); 
} 

if ($rating_from_select) { 
$args['metaquery'][] = array(
    'key' => 'wine_rating', 
    'value' => $rating_from_select, 
    'compare' => '>=' 
); 
} 

if ($rating_to_select) { 
$args['metaquery'][] = array(
    'key' => 'wine_rating', 
    'value' => $rating_to_select, 
    'compare' => '<=' 
); 
} 
); 
?> 

現在,出於某種原因,這將導致自定義字段下拉選擇到不行。無論用戶輸入什麼內容,所有搜索結果都會返回,所有我指的是該自定義帖子類型的所有帖子。

我上面的代碼有什麼問題嗎? 我似乎無法找出這一個!

順便說一句,我使用下面的代碼正確定義所有的變量(我認爲)在搜索結果的PHP模板的頂部:

<?php 
$vintage_select = $_POST['vintage_select']; 
$region_select = $_POST['region_select']; 

$winery_select = $_POST['winery_select']; 
$score_100_from = $_POST['score_100_from']; 
$score_100_to = $_POST['score_100_to']; 
$score_20_from = $_POST['score_20_from']; 
$score_20_to = $_POST['score_20_to']; 
$rating_from_select = $_POST['rating_from_select']; 
$rating_to_select = $_POST['rating_to_select']; 
?> 

回答

1

第二個代碼片段被搞砸。你在query_posts()函數的參數列表中有條件。

在將它傳遞給$args數組之前,首先創建並填充您的'meta_query'項目。

實施例:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 

$metaList = [ 
    'vintage_select'=>['vintage','LIKE'], 
    'region_select'=>['full_list_regions','LIKE'], 
    'winery_select'=>['producer_name','LIKE'], 
    'score_100_from'=>['S100','>='], 
    'score_100_to'=>['S100','<='], 
    'score_20_from'=>['S20','>='], 
    'score_20_to'=>['S20','<='], 
    'rating_from_select'=>['wine_rating','>='], 
    'rating_to_select'=>['wine_rating','<='], 

]; 

$metaQuery = []; 

foreach ($metaList as $key=>$item) 
    if(isset($$key) && $$key) $metaQuery[] = ['key'=>$item[0], 'compare'=>$item[1], 'value'=>$$key]; 

$args = [ 
    'post_type' => 'wine-note', 
    'posts_per_page' => '10', 
    'paged' => $paged, 
    'metaquery'=> $metaQuery 
]; 

query_posts($args); 
+0

將除去 '的$ args' 'query_posts()' 並將其添加到 'meta_query =>的$ args =陣列(' 解決問題 – Rob

+0

我已經改變的代碼來「query_posts(陣列( \t 'post_type'=> '酒音符', \t 'posts_per_page'=> '10', \t '尋呼'=> $尋呼, \t 'meta_query'=>的$ args =陣列()));'but no good! – Rob

+0

Added a example。 – diafol

相關問題