2013-11-23 31 views
1

我正在wordpress中成功運行查詢。查詢如下。單個查詢(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,因爲我有很多基於這個查詢的其他查詢。

在此先感謝!

+0

你能更具體的,而不是「它不工作」?什麼不工作? –

+0

已經在這裏提出並回答了? http://stackoverflow.com/questions/20160027/meta-key-and-meta-value-query-in-wordpress –

回答

0

如果不添加您自己的查詢,請嘗試擴展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); 
0
$args = array(
    'post_type' => 'property', 
    'post_status' => 'publish', 
    'posts_per_page' => 10, 
    'order' => 'ASC', 
    'meta_query' => array(
     array(
      'key' => 'property_amount', 
      'value' => '1', 
      'compare' => '=', 
     ), 
     array(
      'key' => 'property_land_type', 
      'value' => array('L','H','C'), 
      'compare' => 'IN', 
     ) 
    ) 
); 

$query = new WP_Query($args); 

if ($query->have_posts()) : 
    while ($query->have_posts()) : $query->the_post(); 
      echo $post_id = get_the_ID(); 
    endwhile; 
endif;