如果我举个例子就更容易了:
我有一个自定义的帖子类型MYPOSTTYPE, 有3种分类法TAX_1, TAX_2 和TAX_3
如果我有3个术语:“TERM\\u TAX\\u 1”TAX_1, \'TERM\\u TAX\\u 2\'英寸TAX_2, 和中的“TERM\\u TAX\\u 3”TAX_3, 我如何计算有多少MYPOSTTYPE具有SQL查询和$wpdb的所有3个术语?
我试过这样的东西,但没用。。。计数器始终为0
SELECT COUNT($wpdb->posts.ID) AS counter
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)
LEFT JOIN $wpdb->terms AS tax_one ON ($wpdb->term_taxonomy.term_id = tax_one.term_id)
LEFT JOIN $wpdb->terms AS tax_two ON ($wpdb->term_taxonomy.term_id = tax_two.term_id)
LEFT JOIN $wpdb->terms AS tax_three ON ($wpdb->term_taxonomy.term_id = tax_three.term_id)
WHERE 1=1
AND $wpdb->posts.post_type = \'MYPOSTTYPE\'
AND $wpdb->posts.post_status = \'publish\'
AND $wpdb->term_taxonomy.taxonomy IN (\'TAX_1\', \'TAX_2\', \'TAX_3\')
AND tax_one.slug = \'TERM_TAX_1\'
AND tax_two.slug = \'TERM_TAX_1\'
AND tax_three.slug = \'TERM_TAX_1\'
UPDATE: 我也试过
tax_query, 但我有这个问题
Problem with get_posts, tax_query and counting the number of posts所以我想尝试直接查询数据库
SO网友:kaiser
执行tax-query 然后count
结果。无需使用几十个JOINS
.
$posts = new WP_Query( array(
\'post_type\' => \'MYPOSTTYPE\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'TAX_1\',
\'field\' => \'slug\',
\'terms\' => array( \'TERM_TAX_1\' ),
\'operator\' => \'IN\'
),
array(
// etc.
)
),
\'post_status\' => \'publish\'
);
prinft(
\'<h3>COUNT: %s</h3>\'
,count( $posts )
);