单个查询中的多个元键和元值(wordpress)

时间:2013-11-23 12:49:01

标签: mysql 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,因为我有很多基于这个查询的其他查询。

提前致谢!!!

2 个答案:

答案 0 :(得分: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 );

答案 1 :(得分: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;