Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

$args=array(
'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

1 Answer 1

up vote 1 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(
        $wpdb->prepare( 
        "
        SELECT COUNT(ID) FROM $wpdb->posts 
        WHERE post_status = %s 
        AND post_author = %d",
        $post_status,
        $user_id
        )
    );
    return ($count) ? $count : 0;
}

Usage:

<?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

 
discard

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.