我正在试验搜索、多准则和排序。假设我们有一个房地产网站,所有帖子都有2个自定义字段(price
和surface
), 我们希望访问者能够通过输入:
最低价格,最高价格,最小表面,最大表面,这将完成以下工作:
function mwm_register_search_filters( $query ) {
// Only on archive post listings
if( $query->is_archive ) {
$filters_a = array();
if( !empty( $_GET[\'price_min\'] ) ) $filters_a[] = array( \'key\' => \'price\', \'value\' => $_GET[\'price_min\'], \'compare\' => \'>=\', \'type\' => \'numeric\' );
if( !empty( $_GET[\'price_max\'] ) ) $filters_a[] = array( \'key\' => \'price\', \'value\' => $_GET[\'price_max\'], \'compare\' => \'<=\', \'type\' => \'numeric\' );
if( !empty( $_GET[\'surface_min\'] ) ) $filters_a[] = array( \'key\' => \'surface\', \'value\' => $_GET[\'surface_min\'], \'compare\' => \'>=\', \'type\' => \'numeric\' );
if( !empty( $_GET[\'surface_max\'] ) ) $filters_a[] = array( \'key\' => \'surface\', \'value\' => $_GET[\'surface_max\'], \'compare\' => \'<=\', \'type\' => \'numeric\' );
if( !empty( $filters_a ) ) $query->set( \'meta_query\' , $filters_a );
}
return $query;
}
add_filter( \'pre_get_posts\' , \'mwm_register_search_filters\' );
这很好,而且效果很好。但接下来是订购。。AFAIK Wordpress只允许按一个meta\\u键/meta\\u值排序。这很好,但当您需要按多个标准排序时,这还不够好。是否有订购方式,例如:
首先按价格ASC订购,在同等价格范围内,按表面ASC订购