我想根据一个类别名称统计帖子的数量(公共、私有、受保护)blog
. 无论我使用什么类别,都能得到所有的帖子$total_pages = wp_count_posts()->publish;
但我不知道如何计算类别名称的帖子数量blog
我想根据一个类别名称统计帖子的数量(公共、私有、受保护)blog
. 无论我使用什么类别,都能得到所有的帖子$total_pages = wp_count_posts()->publish;
但我不知道如何计算类别名称的帖子数量blog
要计数only published 发布类别,很容易获得count
类别对象的参数。但是,请注意,如果类别与某些CPT关联,则计数是属于特定术语的所有职位的总数,无论它们是什么post\\u类型。
$cat = get_category(\'blog\');
$post_with_blog_category = $cat->count;
如果您还想计算其他post状态,唯一的方法是运行WP_Query
看看找到的帖子数量。您可以使用函数使其更加灵活和可重用:function count_posts_by_tax( $term = \'\', $tax = \'category\', $type = \'post\' ) {
if ( empty( $term ) ) return false;
$q = new_WP_Query( array(
\'post_type\' => $type,
\'post_status\' => \'any\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => $tax,
\'terms\' => array($term),
\'field\' => is_int($term) ? \'id\' : \'slug\'
)
)
) );
if ( ! $q->found_posts ) return 0;
$statuses = wp_list_pluck( $q->posts, \'post_status\' );
return (object) array_count_values( $statuses );
}
现在您可以这样使用它:$count = count_posts_by_tax(\'blog\');
$published_blog_posts = $count->publish;
$private_blog_posts = $count->private;
$prending_blog_posts = $count->pending;
$draft_blog_posts = $count->draft;
函数足够灵活,可以用于除category以外的其他分类法,也可以用于除standard post以外的其他post类型,只需将第2个和第3个参数传递给函数即可。Please note 该函数运行SQL查询来检索所有状态下的所有帖子……如果有数百篇帖子,它可能会非常慢,并且会持续影响页面加载。
如果您有很多帖子,并且需要根据类别和不同的帖子状态对其进行计数,我建议您创建一个自定义函数,该函数将挂钩到类别/帖子关联中,并在发生时更新db中的值,可能是选项或瞬态。一旦需要不同post状态的计数,还应该挂接post状态转换以使计数一致。当然,这是一项更长、更复杂的工作,但这项工作的绩效提升是值得的,尤其是如果你有(或计划有)大量的职位。
以下是另一种灵活的统计帖子的方法:
WP_Query()
所以它只会返回results count 而不是发布对象。$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'category_name\' => \'reykjavik\',
);
$q = new WPSE_121749_Query_Count( $args );
echo $q->count();
您可以在哪里更改$args
根据您的喜好和WPSE_121749_Query_Count
是的扩展WP_Query
.echo $q->count();
是95
自从print_r( $q->posts );
是:Array ( [0] => 95 )
它不包含任何post对象,因为我们只需要count.WP_Query
我使用了三个过滤器和一个操作,但可能有更好的方法使用其他挂钩来实现这一点。以下是该课程的第一个版本:
/**
* Class WPSE_121749_Query_Count
*
*/
class WPSE_121749_Query_Count extends WP_Query
{
public function __construct( $args = array() )
{
add_filter( \'posts_request\', array( $this, \'posts_request\' ) );
add_filter( \'posts_orderby\', array( $this, \'posts_orderby\' ) );
add_filter( \'post_limits\', array( $this, \'post_limits\' ) );
add_action( \'pre_get_posts\', array( $this, \'pre_get_posts\' ) );
parent::__construct( $args );
}
public function count()
{
if( isset( $this->posts[0] ) )
return $this->posts[0];
return \'\';
}
public function posts_request( $request )
{
remove_filter( current_filter(), array( $this, __FUNCTION__ ) );
return sprintf( \'SELECT COUNT(*) FROM ( %s ) as t\', $request );
}
public function pre_get_posts( $q )
{
$q->query_vars[\'fields\'] = \'ids\';
remove_action( current_filter(), array( $this, __FUNCTION__ ) );
}
public function post_limits( $limits )
{
remove_filter( current_filter(), array( $this, __FUNCTION__ ) );
return \'\';
}
public function posts_orderby( $orderby )
{
remove_filter( current_filter(), array( $this, __FUNCTION__ ) );
return \'\';
}
}
如何才能做到这一点?将wp\\u list\\u类别上的帖子计数替换为“Has Posts”,而不是数字,如果没有,则替换为“No Posts”。