我试图根据州的位置查询状态。来自map_location元键的地址字符串示例:
我无法弄清楚如何让'map_location'值='新南威尔士州'或'新南威尔士'
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'listing_status',
'value' => 'current'
),
array(
'key' => 'map_location',
'compare' => 'LIKE',
'value' => 'NSW'
)
)
);
$state_posts = new WP_Query($args);
答案 0 :(得分:1)
您可以这样做: -
$args = array(
'post_type' => 'post',
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'listing_status',
'value' => 'current'
),
array(
'key' => 'map_location',
'compare' => 'LIKE',
'value' => 'NSW'
),
array(
'key' => 'map_location',
'compare' => 'LIKE',
'value' => 'NEW SOUTH WALES'
)
)
);
答案 1 :(得分:0)
在这种情况下,问题是部分匹配,分两步执行:找到正确的帖子,然后查询它们。在使用分页编辑后尝试此(经过测试)的解决方案(在第二次查询时再次会更容易,但会降低性能):
$meta_key1 = 'map_location';
$meta_value1 = '%NSW%';
$meta_key2 = 'map_location';
$meta_value2 = '%NEW SOUTH WALES%';
$meta_key3 = 'listing_status';
$meta_value3 = 'current';
if (get_query_var('paged')) $page_no = get_query_var( 'paged' ) - 1; else $page_no = 0;
$posts_per_page = 1;
global $wpdb;
$postids = $wpdb->get_col( $wpdb->prepare(
"
SELECT key2.post_id
FROM $wpdb->postmeta key2
INNER JOIN $wpdb->postmeta key1
ON key1.post_id = key2.post_id
AND ((key1.meta_key = %s AND key1.meta_value LIKE %s) OR (key1.meta_key = %s AND key1.meta_value LIKE %s))
WHERE key2.meta_key = %s AND key2.meta_value = %s LIMIT %d , %d
",
$meta_key1,
$meta_value1,
$meta_key2,
$meta_value2,
$meta_key3,
$meta_value3,
$page_no,
$posts_per_page
) );
$the_query = new WP_Query( array('post__in' => $postids ) );
BTW,没有部分匹配解决方案要简单得多:
$args = array(
'meta_query' => array(
array(
'key' => 'listing_status',
'value' => 'current',
'meta_compare' => '='
),
array(
'key' => 'map_location',
'value' => array('NEW SOUTH WALES','BATEMANS BAY NEW SOUTH WALES 2536 Australia'),
'meta_compare' => 'IN'
)
)
);
$the_query = new WP_Query($args);