代码之家  ›  专栏  ›  技术社区  ›  Reece

使用多个键筛选查询帖子

  •  0
  • Reece  · 技术社区  · 6 年前

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

    尝试一次:

    <?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 回复  |  直到 6 年前
        1
  •  1
  •   Fernando Souza    6 年前

    您可以使用“关系”选项来确定如何构建查询。此外,还可以嵌套meta\u查询子句。在第一个示例中,查询可以是这样的:

    <?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\u status=待售且(镇或县或邮编)=$location的帖子。

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