查询过帐使用多个键过滤

时间:2018-05-03 15:52:54

标签: php html wordpress filtering

您好我正在开发一个属性网站,我希望用户能够按位置进行过滤。我有一个文本输入供用户执行此操作。在后端,我有三个高级自定义字段,每个字段指定位置的不同部分,例如;镇,县和邮政编码。现在我需要用户能够将一个城镇,县或邮政编码输入到一个输入框中,然后我想存储该值并使用它来检查所有字段。我尝试了两种不起作用的方法;

尝试一个:

<?php 
                    if($_GET['min_price'] && !empty($_GET['min_price'])){
                        $min_price = $_GET['min_price'];
                    }else{
                        $min_price = 0;
                    }

                    if($_GET['max_price'] && !empty($_GET['max_price'])){
                        $max_price = $_GET['max_price'];
                    }else{
                        $max_price = 10000000;
                    }

                    if($_GET['bedrooms'] && !empty($_GET['bedrooms'])){
                        $bedrooms = $_GET['bedrooms'];
                    }

                    if($_GET['location'] && !empty($_GET['location'])){
                        $location = $_GET['location'];
                    }

                $posts = get_posts(array(
                    'posts_per_page'    =>  -1,
                    'post_type'         =>  'property',
                    'orderby'           =>  'date',
                    'meta_query'        =>  array(
                        array(
                            'key'       => 'property_status',
                            'value'     => 'For Sale'
                        ),

                        array(
                            'key'       => 'town',
                            'value'     => $location,
                            'compare'   => 'LIKE'
                        ),

                        array(
                            'key'       => 'county',
                            'value'     => $location,
                            'compare'   => 'LIKE'
                        ),

                        array(
                            'key'       => 'postcode',
                            'value'     => $location,
                            'compare'   => 'LIKE'
                        )
                    )

                ));

尝试两个:

<?php 
                    if($_GET['min_price'] && !empty($_GET['min_price'])){
                        $min_price = $_GET['min_price'];
                    }else{
                        $min_price = 0;
                    }

                    if($_GET['max_price'] && !empty($_GET['max_price'])){
                        $max_price = $_GET['max_price'];
                    }else{
                        $max_price = 10000000;
                    }

                    if($_GET['bedrooms'] && !empty($_GET['bedrooms'])){
                        $bedrooms = $_GET['bedrooms'];
                    }

                    if($_GET['location'] && !empty($_GET['location'])){
                        $location = $_GET['location'];
                    }

                $posts = get_posts(array(
                    'posts_per_page'    =>  -1,
                    'post_type'         =>  'property',
                    'orderby'           =>  'date',
                    'meta_query'        =>  array(
                        array(
                            'key'       => 'property_status',
                            'value'     => 'For Sale'
                        ),

                        array(
                            'key'       => 'property_price',
                            'type'      => 'NUMERIC',
                            'value'     => array($min_price, $max_price),
                            'compare'   => 'BETWEEN'
                        ),

                        array(
                            'key'       => 'bedrooms',
                            'value'     => $bedrooms,
                            'compare'   => 'LIKE'
                        ),

                        array(
                            'key'       => array('town', 'county', 'postcode'),
                            'value'     => $location,
                            'compare'   => 'LIKE'
                        )
                    )

                ));

HTML:

<form action="<?php the_permalink(); ?>" method="get">
                <label>Min:</label>
                <input type="number" name="min_price"><br>

                <label>Max:</label>
                <input type="number" name="max_price"><br>

                <label>Bedrooms:</label><br>
                <label>1</label><input type="radio" name="bedrooms" value="1">
                <label>2</label><input type="radio" name="bedrooms" value="2">
                <label>3</label><input type="radio" name="bedrooms" value="3">
                <label>4</label><input type="radio" name="bedrooms" value="4">
                <label>5</label><input type="radio" name="bedrooms" value="5">
                <label>6+</label><input type="radio" name="bedrooms" value="6+">

                <label>Location</label><br>
                <input type="text" name="location">

                <input type="submit">
            </form>

1 个答案:

答案 0 :(得分:1)

您可以使用'relation'选项来确定查询的构建方式。此外,meta_query子句可以嵌套。在您的第一个示例中,查询可能是这样的:

<?php 
if($_GET['min_price'] && !empty($_GET['min_price'])){
  $min_price = $_GET['min_price'];
}else{
  $min_price = 0;
}

if($_GET['max_price'] && !empty($_GET['max_price'])){
   $max_price = $_GET['max_price'];
}else{
   $max_price = 10000000;
}

if($_GET['bedrooms'] && !empty($_GET['bedrooms'])){
   $bedrooms = $_GET['bedrooms'];
}

if($_GET['location'] && !empty($_GET['location'])){
   $location = $_GET['location'];
}

$posts = get_posts(array(
   'posts_per_page'    =>  -1,
   'post_type'         =>  'property',
   'orderby'           =>  'date',
   'meta_query'        =>  array(
       'relation' => 'AND',
        array(
           'key'       => 'property_status',
           'value'     => 'For Sale'
        ),
        array(
           'relation' => 'OR',
           array(
              'key'       => 'town',
              'value'     => $location,
              'compare'   => 'LIKE'
           ),
           array(
              'key'       => 'county',
              'value'     => $location,
              'compare'   => 'LIKE'
           ),
           array(
              'key'       => 'postcode',
              'value'     => $location,
              'compare'   => 'LIKE'
           )
       )
   ) 
));

在这个查询中,我们想要的是,我们希望发布的帖子为property_status = For Sale AND(town OR county或postcode)= $ location。

您可以在此处的“多个自定义字段处理”部分中找到更多详细信息: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters