【问题标题】:Query Posts filtering with multiple keys使用多个键查询帖子过滤
【发布时间】:2018-05-04 03:37:20
【问题描述】:

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

尝试一:

<?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>

【问题讨论】:

    标签: php html wordpress filtering


    【解决方案1】:

    您可以使用“关系”选项来确定如何构建查询。此外,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 OR postcode) = $location。

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

    【讨论】:

      猜你喜欢
      • 2021-09-27
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      • 2022-01-14
      • 2018-08-04
      • 2019-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多