2014-11-03 62 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'; 
}