我想你最好自己写这样的东西。
请看一下:http://www.catalysthomes.co.uk/homes-for-sale/
属性被加载到CPT中,我在侧栏中有自己的自定义搜索。其中,搜索包括许多内容,如分类法、自定义字段和按日期价格排序等。
那么我该如何实现这一点呢?我将表单提交到页面模板,然后在那里处理帖子数据,并根据搜索条件构建新的WP\\U查询。我使用会话存储搜索变量,以便对结果分页。
WP\\u查询功能非常强大。看一看:http://codex.wordpress.org/Class_Reference/WP_Query
在那里你可以使用meta_query
查询多个自定义字段并使用tax_query
查询分类法,还有更多。下面是我的想法。
Template File:
<?php
$temp = $wp_query;
$wp_query = NULL;
$args = array();
?>
<?php include("functions/Homes-for-sale/propertyrawresults.php"); ?>
<?php include("functions/Homes-for-sale/propertysearchresults.php"); ?>
<?php
$args[\'post_type\'] = "homes-for-sale";
$args[\'showposts\'] = 10;
$args[\'paged\'] = $paged;
$wp_query = new WP_Query($args);
?>
<?php include("functions/Homes-for-sale/propertylistlayout.php"); ?>
Raw Results
<?php
if($_POST[\'sortby\']) {
$_SESSION[\'prop_selectedsortby\'] = $_POST[\'sortby\'];
}
switch($_SESSION[\'prop_selectedsortby\']) {
case "name-asc": $args[\'order\'] = "ASC"; $args[\'orderby\'] = "title"; break;
case "name-desc": $args[\'orderby\'] = "title"; break;
case "price-asc": $args[\'order\'] = "ASC"; $args[\'orderby\'] = "meta_value_num"; $args[\'meta_key\'] = "chb_homes_for_sale_specifics_fmv"; break;
case "price-desc": $args[\'orderby\'] = "meta_value_num"; $args[\'meta_key\'] = "chb_homes_for_sale_specifics_fmv"; break;
case "date-asc": $args[\'order\'] = "ASC"; break;
default: /* No need to set arguments here as wp query defaults */ break;
}
$selectedsortby[$_SESSION[\'prop_selectedsortby\']] = " selected=\\"selected\\"";
?>
Search Results
<?php
if( ! empty( $_SESSION[\'s_property_ptype\'] ) ) {
$args[\'meta_query\'][] = array(
\'key\' => \'chb_homes_for_sale_types_nbrs\',
\'value\' => $_SESSION[\'s_property_ptype\']
);
}
if( ! empty( $_SESSION[\'s_property_development\'] ) ) {
$args[\'meta_query\'][] = array(
\'key\' => \'chb_homes_for_sale_ofdevelopment\',
\'value\' => $_SESSION[\'s_property_development\']
);
}
if( isset( $_SESSION[\'s_property_area\'] ) && 0 != $_SESSION[\'s_property_area\'] ) {
$args[\'tax_query\'][] = array(
\'taxonomy\' => \'areas\',
\'field\' => \'id\',
\'terms\' => array( (int) $_SESSION[\'s_property_area\'] ),
);
}
$args[\'meta_query\'][] = array(
\'key\' => \'chb_homes_for_sale_specifics_bedrooms\',
\'value\' => $_SESSION[\'s_property_bedrooms_min\'],
\'compare\' => \'>=\',
\'type\' => \'SIGNED\'
);
$args[\'meta_query\'][] = array(
\'key\' => \'chb_homes_for_sale_specifics_bedrooms\',
\'value\' => $_SESSION[\'s_property_bedrooms_max\'],
\'compare\' => \'<=\',
\'type\' => \'SIGNED\'
);
$args[\'meta_query\'][] = array(
\'key\' => \'chb_homes_for_sale_specifics_bathrooms\',
\'value\' => $_SESSION[\'s_property_bathrooms_min\'],
\'compare\' => \'>=\',
\'type\' => \'SIGNED\'
);
$args[\'meta_query\'][] = array(
\'key\' => \'chb_homes_for_sale_specifics_bathrooms\',
\'value\' => $_SESSION[\'s_property_bathrooms_max\'],
\'compare\' => \'<=\',
\'type\' => \'SIGNED\'
);
$args[\'meta_query\'][] = array(
\'key\' => \'chb_homes_for_sale_specifics_fmv\',
\'value\' => $_SESSION[\'s_property_min_price\'],
\'compare\' => \'>=\',
\'type\' => \'SIGNED\'
);
$args[\'meta_query\'][] = array(
\'key\' => \'chb_homes_for_sale_specifics_fmv\',
\'value\' => $_SESSION[\'s_property_max_price\'],
\'compare\' => \'<=\',
\'type\' => \'SIGNED\'
);
?>
List Layout只有一个标准的WP循环来显示帖子摘录和信息。