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'];
?>
將除去 '的$ args' 'query_posts()' 並將其添加到 'meta_query =>的$ args =陣列(' 解決問題 – Rob
我已經改變的代碼來「query_posts(陣列( \t 'post_type'=> '酒音符', \t 'posts_per_page'=> '10', \t '尋呼'=> $尋呼, \t 'meta_query'=>的$ args =陣列()));'but no good! – Rob
Added a example。 – diafol