我在wordpress中成功运行查询。查询如下。
SELECT wp_posts.*, wp_postmeta.meta_value
FROM wp_posts, wp_postmeta, wp_term_relationships, wp_terms
WHERE term_id = '12'
AND term_taxonomy_id = '12'
AND ID = post_id
AND ID = object_id
AND post_type = 'property'
AND post_status = 'publish'
AND meta_key = 'property_amount'
AND replace(replace(meta_value, ',', ''), '"', '') >= 1
GROUP BY wp_posts.ID
ORDER BY replace(replace(meta_value, ',', ''), '"', '') DESC LIMIT 0, 10
但是我想在上面的查询中再添加一个meta_key及其值条件,所以我将查询更改为此
SELECT wp_posts.*, wp_postmeta.meta_value
FROM wp_posts, wp_postmeta, wp_term_relationships, wp_terms
WHERE term_id = '12'
AND term_taxonomy_id = '12'
AND ID = post_id
AND ID = object_id
AND post_type = 'property'
AND post_status = 'publish'
AND ((wp_postmeta.meta_key = 'property_amount'
AND wp_postmeta.meta_value) >= '1'
AND (wp_postmeta.meta_key = 'property_land_type'
AND wp_postmeta.meta_value IN ('H', 'C')))
GROUP BY wp_posts.ID
第一条查询中的下一行是多余的
meta_key="property_land_type" and meta_value in ('L','H','C')
但这是行不通的。怎么做。这次我无法编写WP_Query,因为我还有很多其他基于此查询的查询。
提前致谢!!!
最佳答案
在不添加您自己的查询的情况下,尝试扩展wordpress查询。您可以自定义以下代码,
$args = array(
'post_type' => 'property',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'property_amount',
'value' => '1',
'compare' => 'LIKE',
),
array(
'key' => 'property_land_type',
'value' => 'L',
'compare' => 'LIKE',
)
)
);
$query = new WP_Query( $args );
关于mysql - 单个查询中的多个meta键和meta值(wordpress),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20162485/