【问题标题】:Custom Search Query in relation to search form与搜索表单相关的自定义搜索查询
【发布时间】:2012-10-02 10:28:43
【问题描述】:

我有一些自定义字段作为新帖子类型的元数据 - 属性(用于房地产经纪人),因此希望按卧室数量、最小值/最大值和位置进行搜索。我有一个表单,其中每个字段都有多个下拉列表:

位置、最小值、最大值、卧室

另外,我在帖子本身上有元框,所以一个用于价格、卧室、位置和 property_type 的分类类型 - 租金、销售和商业。

我在网上找到了这段代码,但不知道如何操作它以获取表单所取的任何值?

$args = array(
    'post_type' => 'product',
    'meta_query' => array(
        array(
            'key' => 'location',
            'value' => '[LOCATION HERE]',
            'compare' => 'NOT LIKE'
        ),
        array(
            'key' => 'price',
            'value' => '[PRICE HERE FROM FORM]',
            'type' => 'numeric',
            'compare' => 'BETWEEN'
        )
    )
 );
$query = new WP_Query( $args );

另外,我知道搜索查询是在 function.php 上进行的,但我是从表单所在的位置调用它,还是从输出结果的位置调用它? IE。我的主页还是我的搜索页面?

希望有人能帮忙

【问题讨论】:

    标签: php mysql wordpress


    【解决方案1】:

    使用此代码

    $args = array(
    'post_type' => 'Properties',
    'meta_query' => array(
        array(
            'key' => 'location',
            'value' => '[LOCATION HERE]',
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'min_value',
            'value' => '[min value here]',
            'type' => 'numeric',
            'compare' => 'BETWEEN'
        )
        array(
            'key' => 'max_value',
            'value' => '[max value here]',
            'type' => 'numeric',
            'compare' => 'BETWEEN'
        )
        array(
            'key' => 'bedrooms',
            'value' => '[bedroom here]',
            'compare' => 'LIKE'
        ),
      )
     );
     $query = new WP_Query( $args );
    

    你必须在你的searchpage....中打电话给这个。

    【讨论】:

    • 嗨,那么它是如何获取表单的值的呢?假设您的表单字段 id 是“卧室”,它会成为搜索数组的“值”吗?
    • 您必须使用表单字段name 而不是id
    • 所以表单的名称与数组的键匹配,这就是它们的链接方式?
    【解决方案2】:

    感谢 Yogesh 的帮助,我修改了您的答案以使其看起来有效:

                <?php $args = array(
                        'post_type' => 'Property',
                        'property_type'=>$_GET['type'],
                        'meta_query' => array(
                            'relation' => 'AND',
                            array(
                                'key' => '_property_info_location',
                                'value' => Cuztom::uglify($_GET['location']),
                            ),
                            array(
                                'key' => '_property_info_bedrooms',
                                'value' => $_GET['bedrooms'],
                            ),
                            array(
                                'key' => '_property_info_price',
                                'value' => $_GET['max_value'],
                                'compare' => '<=',
                                'type' => 'numeric',
                            ),
                            array(
                                'key' => '_property_info_price',
                                'value' => $_GET['min_value'],
                                'compare' => '>=',
                                'type' => 'numeric',
                            ),
                        ),
                    );
                    $the_query = new WP_Query( $args );
                    ?>
    

    【讨论】:

      猜你喜欢
      • 2016-05-13
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      相关资源
      最近更新 更多