从 meta_key 等于的类别中获取帖子数?

get number of posts from category where meta_key is equal to?

我正在尝试创建一个自定义 mysql 查询,该查询计算两件事。

首先,它应该计算 meta_key betting_status 值不等于 "pending"

的类别中的帖子数

然后它应该计算 meta_key betting_status' 值等于 yes 的类别中的帖子数。

到目前为止我已经完成了这个但是它根本没有输出任何东西:

$wombo_query = "SELECT count(DISTINCT $wpdb->postmeta.post_id) FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) AND
WHERE $wpdb->postmeta.meta_key = 'betting_status'
AND $wpdb->posts.meta_value = 'yes'
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = 106
";

试试这个。

SELECT count(DISTINCT wp_postmeta.`post_id`) 
  FROM wp_posts
       LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
       LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
       LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) 
 WHERE wp_postmeta.meta_key       = 'betting_status'
    AND wp_postmeta.meta_value    = 'yes'
    AND wp_posts.post_status      = 'publish'
    AND wp_term_taxonomy.taxonomy = 'category'
    AND wp_term_taxonomy.term_id  = 106;

由于我的数据库中的值与您的不同,所以这 returns 0 行对我来说,但它确实执行了。

干杯! =C=