与搜索表单相关的自定义搜索查询

时间:2012-10-02 10:28:43

标签: php mysql wordpress

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

location,min_value,max_value,bedrooms

另外,我在帖子本身上有元框,因此有一个用于价格,卧室,位置和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上进行的,但我是从表单所在的位置调用它还是在输出结果的地方调用它?即。我的主页或我的搜索页面?

希望有人可以提供帮助

2 个答案:

答案 0 :(得分:0)

使用此代码

$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 ....

中调用它

答案 1 :(得分:0)

感谢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 );
                ?>
相关问题