我一直在构建自定义搜索。搜索具有许多下拉菜单,用户可以将这些下拉列表留空,也可以从多个选项中进行选择。此数据保存在“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'];
?>
答案 0 :(得分:1)
第二个代码片段搞砸了。您在query_posts()
函数的参数列表中有条件。
创建并填充您的元数据&#39;在将它传递给$args
数组之前先将项目。
示例:
$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 );