Wordpress meta_query按区域搜索(lat,lng)

时间:2015-10-10 22:47:32

标签: wordpress

我有区域(lat0,lat1,lng0,lng1)。为什么我的wp_query无法正常工作?

        $new_query->query( array(
            'post_type' => 'object',
            'meta_query' => array(
        array(
            'relation' => 'AND',
        array(
            'key' => 'lat',
            'compare' => 'BETWEEN',
            'value' => array($lat0, $lat1),
            'type' => 'DECIMAL',
        ),
        array(
            'key' => 'lng',
            'compare' => 'BETWEEN',
            'value' => array($lng0, $lng1),
            'type' => 'DECIMAL',
        )
    ),
                        ),

                    )
                );

如果我使用,我会得到一些结果:

        array(
            'key' => 'lat',
            'compare' => '<=',
            'value' => $lat0,
            'type' => 'DECIMAL',
        ),

但是这段代码不起作用:

         array(
            'relation' => 'AND',
        array(
            'key' => 'lat',
            'compare' => '>=',
            'value' => $lat0,
            'type' => 'DECIMAL',
        ),
            array(
            'key' => 'lat',
            'compare' => '<=',
            'value' => $lat1,
            'type' => 'DECIMAL',
        )
    ),

我可以按区域使用wp_query进行搜索。如果这是错误的方式,我能做什么?

1 个答案:

答案 0 :(得分:0)

固定

                    global $wpdb;
        $querystr = "
        SELECT *
        FROM $wpdb->posts
        INNER JOIN $wpdb->postmeta m1
          ON ( $wpdb->posts.ID = m1.post_id )
        INNER JOIN $wpdb->postmeta m2
          ON ( $wpdb->posts.ID = m2.post_id )
        INNER JOIN $wpdb->postmeta m3
          ON ( $wpdb->posts.ID = m3.post_id )
        WHERE
        $wpdb->posts.post_type = 'object'
        AND $wpdb->posts.post_status = 'publish'
        AND ( m1.meta_key = 'lat' AND m1.meta_value < $lat0 )
        AND ( m1.meta_key = 'lat' AND m1.meta_value > $lat1 )
        AND ( m2.meta_key = 'lng' AND m2.meta_value < $lng0 )
        AND ( m2.meta_key = 'lng' AND m2.meta_value > $lng1 )
        AND ( m3.meta_key = 'city' AND m3.meta_value = $city_string )
        GROUP BY $wpdb->posts.ID
        ORDER BY $wpdb->posts.post_date
        DESC;
        ";
         $pageposts = $wpdb->get_results($querystr, OBJECT);