你好@Rich:
欢迎使用WordPress答案。
这个\'post_category\'
字段不再使用,已被一组“分类法”表所取代。如果您使用的是direct SQL,则如下所示:
$sql = <<<SQL
SELECT
COUNT(*)
FROM
{$wpdb->posts}
INNER JOIN {$wpdb->term_relationships} ON {$wpdb->term_relationships}.object_id={$wpdb->posts}.ID
INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id={$wpdb->term_relationships}.term_taxonomy_id
WHERE 1=1
AND {$wpdb->posts}.post_status = \'publish\'
AND {$wpdb->posts}.post_type = \'post\'
AND {$wpdb->posts}.post_name NOT LIKE \'%revision%\'
AND {$wpdb->posts}.post_name NOT LIKE \'%autosave%\'
AND {$wpdb->term_taxonomy}.term_id NOT IN ($folio_cat_ids)
SQL;
$numposts = $wpdb->get_var($sql);
然而,如果可以避免在使用WordPress API时编写大量SQL,那么最好使用WordPress API而不是直接SQL,在这种情况下,这是非常可能的。我为你写了一个小班叫
CountPostsCategoriesNotIn
它封装了
\'posts_fields\'
您需要使用的挂钩。调用此类非常简单:
$folio_cat_ids = \'1,2,3\';
$numposts = CountPostsCategoriesNotIn::count($folio_cat_ids);
echo "$numposts\\n";
这是一个可以复制到主题中的类
functions.php
文件或在中使用
.php
您可能正在编写的插件的文件:
class CountPostsCategoriesNotIn {
static function count($categories) {
$posts_fields = array(__CLASS__,\'posts_fields\');
add_action(\'posts_fields\',$posts_fields);
$query = new WP_Query(array(
\'posts_per_page\' => \'-1\',
\'category__not_in\' => $categories,
));
remove_action(\'posts_fields\',$posts_fields);
return $query->post->post_count;
}
static function posts_fields($field_list) {
$field_list = \'COUNT(*) as post_count\';
return $field_list;
}
}