How we count the user draft posts

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.

1 Answer
1

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

Leave a Comment