2014-11-03 67 views
0

我能得到一个特定的类别(3)这样发布的文章数量:获得职位的数量

<?php 
$theID = 3; 
$postsInCat = get_term_by('id','' . $theID . '','category'); 
$postsInCat = $postsInCat->count; 
echo $postsInCat . " posts in this category"; 
?> 

但我现在也需要做在一个单独的声明是获取一个特定的类别(3)只是被删除的帖子的数量。

在此先感谢。

回答

1

可能是您的解决方案是:记住类ID保存在wp_terms表,,从u能得到it.and发布类型是“后” 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; 
+0

布拉沃,这个伎俩。谢谢。 – user3256143 2014-11-03 22:13:21

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; 
+0

感谢。它看起来应该可以工作,但是我收到一个错误,我看不到它发生了什么: '语法错误,意外'=>'(T_DOUBLE_ARROW)' 发生此行: ''posts_per_page'=> -1,' – user3256143 2014-11-03 13:37:55

+0

我的错误。我错过了阵列。请重试 – 2014-11-03 23:22:57

1

你可以做到这一切是使用get_posts作为替代

概念

检索从指定类别的瓦特所有帖子一个查询第i个职位状态trashpublish

接下来你需要返回数组分解成两个阵列,一个用于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'; 
}