WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to use the total posts count currently i am using the this query

'author' => $userID,
'post_type' => 'post',
'post_status' => $poststatus,
'posts_per_page' => 212
<?php echo count_user_posts($args); ?>

$poststatus have 2 options draft and publish. But when i run this query one condition work good and that is publish. but when i try to run draft it not showing correct count of draft posts. So tell me where i am wrong in the query.

share|improve this question
up vote 2 down vote accepted

The function count_user_posts only accepts a user ID so you arguments are never taken in consideration. Here is a simple function to get the count by status

function count_user_posts_by_status($post_status = 'publish',$user_id = 0){
    global $wpdb;
    $count = $wpdb->get_var(
        SELECT COUNT(ID) FROM $wpdb->posts 
        WHERE post_status = %s 
        AND post_author = %d",
    return ($count) ? $count : 0;


<?php echo count_user_posts_by_status('draft',$userID); ?>
share|improve this answer
Thanks for this its work – Adi Mar 12 '13 at 12:19

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.