我有一个可以很好地处理自定义字段的搜索,每个分类法有一个术语。但是,我想从一个分类法“属性类型”中选择多个术语。
我当前的查询如下所示---
/?s=Search&property_city=Any+City&property_state=Any+State&address=&beds=Any&baths=Any&min_price=Any&max_price=Any&property_type=condo&property_type=duplex&min_area=Any&max_area=Any&post_type=property&hide-reports=list-property&property-contract-type=for-sale
选择多个术语时,我需要使用OR运算符--
property_type=condo&property_type=duplex
就像这样-
property_type=condo,duplex
目前在我的主题功能中--
$s = $_GET[\'s\'];
$meta_query=array();
if ($s == __(\'Search\', \'framework\')) {
$query->set(\'s\', \'\');
}
$query->set(\'post_type\', \'property\');
$query->set(\'post_status\',\'publish\');
if (!empty($property_type)) {
$query->set(\'property-type\', $property_type);
}
if (!empty($property_city)) {
$query->set(\'city-type\',$property_city);
}
if (!empty($property_neighborhood)) {
$query->set(\'neighborhood\',$property_neighborhood);
}
if (!empty($property_contract_type)) {
$query->set(\'property-contract-type\', $property_contract_type);
}
完整代码-
https://pastebin.com/LC96j4v5在我的表格上-
<select name="property_type" class="form-control" multiple>
<?php
$terms = get_terms( "property-type", array( \'hide_empty\' => 0 ) );
$count = count($terms);
if ( $count > 0 ){
echo "<option class=\'button\' value=\'Any\'>All</option>";
foreach ( $terms as $term ) {
echo "<option class=\'button\' value=\'" . $term->slug . "\'>" . $term->name . "</option>";
}
}
?>
</select>
最合适的回答,由SO网友:Milan Petrovic 整理而成
您只需对表单进行一次更改,将第一行更改为:
<select name="property_type[]" class="form-control" multiple>
字段的名称必须包含[],以便在到达服务器端时将其视为一个数组。在查询字符串中,它将列为:
property_type[]=condo&property_type[]=duplex
在服务器端,当您阅读本文时,请使用以下内容:
$property_type = (array)$_GET[\'property_type\'];
这可以在
$s = $_GET[\'s\'];
线
请确保在使用之前清理所有输入数据。有关数据清理的更多信息,请参见:Validating Sanitizing and Escaping User Data
SO网友:Sneha Agarwal
您无需对表单进行任何更改。您需要更改主题的函数,以包含一个tax\\u查询,该查询将从分类法属性类型中获取多个术语,其中包含一个关系或
$s = $_GET[\'s\'];
foreach($_GET as $key => $value){
if($key == \'property_type\'){
$item_property_type[\'taxonomy\'] = htmlspecialchars($key);
$item_property_type[\'terms\'] = htmlspecialchars($value);
$item_property_type[\'field\'] = \'slug\';
$list[] = $item_property_type;
}
}
$taxproperty_type = array_merge(array(\'relation\' => \'OR\'), $list);
$meta_query=array();
if ($s == __(\'Search\', \'framework\')) {
$query->set(\'s\', \'\');
}
$query->set(\'post_type\', \'property\');
$query->set(\'post_status\',\'publish\');
if (!empty($property_type)) {
$query->set(\'tax_query\',$taxproperty_type);
}
if (!empty($property_city)) {
$query->set(\'city-type\',$property_city);
}
if (!empty($property_neighborhood)) {
$query->set(\'neighborhood\',$property_neighborhood);
}
if (!empty($property_contract_type)) {
$query->set(\'property-contract-type\', $property_contract_type);
}