我能够获得特定类别(3)上发布的帖子数量,如下所示:
<?php
$theID = 3;
$postsInCat = get_term_by('id','' . $theID . '','category');
$postsInCat = $postsInCat->count;
echo $postsInCat . " posts in this category";
?>
但我现在还需要在单独的声明中做的是获取特定类别(3)的已删除帖子的数量。
提前致谢。
答案 0 :(得分:1)
可能是你的解决方案是:记住类别id保存在wp_terms表中,从中你可以得到它。帖子类型是'post'thx
$args = array(
'posts_per_page' => -1,
'no_found_rows' => true,
'post_status' => 'trash',
'post_type' => 'post',
'category' => 3 );
$post=get_posts($args);
print_r($post);
echo "<br><br>Total Trashed :";
echo $total = ( $post ) ? count( $post ) : 0;
答案 1 :(得分:1)
您可以使用get_posts
作为替代
概念
使用帖子状态trash
或publish
接下来,您需要将返回的数组拆分为两个数组,一个用于trash
个帖子,另一个用于publish
个帖子。使用post_status
对象根据帖子状态对帖子进行排序
您现在可以对两个数组进行计数并回显帖子计数
$args = array(
'posts_per_page' => -1,
'post_status' => array( 'trash', 'publish' ),
'category' 3
);
$posts = get_posts($args);
if( $posts ) {
$trash = [];
$publish = [];
foreach ( $posts as $post ) {
if( $post->post_status == 'trash' ) {
$trash[] = $post;
}else{
$publish[] = $post;
}
}
echo 'There are ' . count($trash) . ' trashed posts </br>';
echo 'There are ' . count($publish) . ' published posts';
}
答案 2 :(得分:0)
使用get_posts()
并计算结果。
// Get trashed post in category 3.
$trashed_posts = get_posts( array(
'posts_per_page' => -1,
'no_found_rows' => true,
'post_status' => trash,
'cat' => 3,
) );
// If posts were found count them else set count to 0.
$trashed_count = ( $trashed_posts ) ? count( $trashed_posts ) : 0;