PHP通配符显示变量

时间:2017-02-03 19:04:05

标签: php wordpress sorting wildcard custom-post-type

我们已经设置了房地产属性的自定义帖子类型和搜索表单,以使用价格,城市,最新等搜索这些自定义帖子类型。我们使用查询变量字符串/ if语句来显示结果,但我们排序选项有问题。我们有三种选择:最新,价格低 - 高和价格高 -

显示默认结果时,排序无法正常运行,但在搜索下拉列表中设置城市时,排序效果很好。

对于城市搜索,我们已准备好此代码:

if (isset($_POST['city'])) {
    $qs_city = get_query_var('city');
}
else {
    $qs_city = " ";

我认为这是由于上面代码的else部分所致。如果没有设置城市,我们需要某种通配符,允许列出列出的所有城市。我们已经尝试了几种不同的东西,但似乎没有任何效果。我对此很新,所以如果有必要,我可以更好地澄清上述任何内容。

这是用于排序的代码:

if (isset($_POST['sort'])) {
    $qs_sort = get_query_var('sort');
    switch ($qs_sort) {
        case 'newest':
            $args = array( 
            'post_type' => 'residential', 
            'posts_per_page' => 10,
            'paged' => $paged,
            'orderby' => 'L_StatusDate',
            'order' => 'DESC',
            'meta_query' => array(
                           array(
                                'key' => 'L_City',
                                'value' => $qs_city,
                                'compare' => 'LIKE'
                           ),
                           array(
                                'key' => 'L_SystemPrice',
                                'value'   => array( $qs_price_min, $qs_price_max ),
                                'type'    => 'numeric',
                                'compare' => 'BETWEEN'
                           ),
                           array(
                               'key' => 'LM_Int1_2',
                               'value' => $qs_beds,
                               'compare' => '>='
                           ),
                           array(
                               'key' => 'LM_Int1_3',
                               'value' => $qs_baths,
                               'compare' => '>='
                           )
                       )
           );
        break;
        case 'price-low':
            $args = array( 
            'post_type' => 'residential', 
            'posts_per_page' => 10,
            'paged' => $paged,
            'orderby' => 'L_SystemPrice',
            'order' => 'ASC',
            'meta_query' => array(
                            array(
                                'key' => 'L_City',
                                'value' => $qs_city,
                                'compare' => 'LIKE'
                           ),
                           array(
                                'key' => 'L_SystemPrice',
                                'value'   => array( $qs_price_min, $qs_price_max ),
                                'type'    => 'numeric',
                                'compare' => 'BETWEEN'
                           ),
                           array(
                               'key' => 'LM_Int1_2',
                               'value' => $qs_beds,
                               'compare' => '>='
                           ),
                           array(
                               'key' => 'LM_Int1_3',
                               'value' => $qs_baths,
                               'compare' => '>='
                           )
                       )
           );
        break;
        case 'price-high':
            $args = array( 
            'post_type' => 'residential', 
            'posts_per_page' => 10,
            'paged' => $paged,
            'orderby' => 'L_SystemPrice',
            'order' => 'DESC',
            'meta_query' => array(
                           //'relation'    => 'AND',
                           array(
                                'key' => 'L_City',
                                'value' => $qs_city,
                                'compare' => 'LIKE'
                           ),
                           array(
                                'key' => 'L_SystemPrice',
                                'value'   => array( $qs_price_min, $qs_price_max ),
                                'type'    => 'numeric',
                                'compare' => 'BETWEEN'
                           ),
                           array(
                               'key' => 'LM_Int1_2',
                               'value' => $qs_beds,
                               'compare' => '>='
                           ),
                           array(
                               'key' => 'LM_Int1_3',
                               'value' => $qs_baths,
                               'compare' => '>='
                           )
                       )
           );
        break;
    }
}
else {
    $args = array( 
            'post_type' => 'residential', 
            'posts_per_page' => 10,
            'paged' => $paged,
            'meta_query' => array(
                           //'relation'    => 'AND',
                           array(
                               'key' => 'L_StatusDate',
                               'orderby' => 'DESC'
                               //set default to sort by newest
                           ),
                           array(
                                'key' => 'L_City',
                                'value' => $qs_city,
                                'compare' => 'LIKE'
                           ),
                           array(
                                'key' => 'L_SystemPrice',
                                'value'   => array( $qs_price_min, $qs_price_max ),
                                'type'    => 'numeric',
                                'compare' => 'BETWEEN'
                           ),
                           array(
                               'key' => 'LM_Int1_2',
                               'value' => $qs_beds,
                               'compare' => '>='
                           ),
                           array(
                               'key' => 'LM_Int1_3',
                               'value' => $qs_baths,
                               'compare' => '>='
                           )
                       )
           );
}

?>

1 个答案:

答案 0 :(得分:0)

您的情况的主要问题是"什么样的应用程序会对结果进行排序?"。看起来你在数据库上使用某种类型。但是哪一个?如果它是类似SQL的数据库, - 只需将$qs_city = " "替换为$qs_city = "%";