【问题标题】:Multiple meta key and meta value in single query (wordpress)单个查询中的多个元键和元值(wordpress)
【发布时间】:2013-11-23 12:49:01
【问题描述】:

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

提前致谢!!!

【问题讨论】:

标签: mysql wordpress


【解决方案1】:

在不添加您自己的查询的情况下,尝试扩展 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 );

【讨论】:

    【解决方案2】:
    $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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      • 2018-08-08
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      相关资源
      最近更新 更多