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