What I am trying to do is modification of this function on codex http://codex.wordpress.org/Function_Reference/count_user_posts Check the title adding post type support bottom of the page. The function is:

function count_user_posts_by_type($userid, $post_type="post") {
  global $wpdb;
  $where = get_posts_by_author_sql($post_type, TRUE, $userid);
  $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
  return apply_filters('get_usernumposts', $count, $userid);
}

What I want is to add post status to the function. So, I assume I have to add a WHERE to the query but not sure how to do it. Any help will be appreciated.

Thanks!

2 Answers
2

Here is a quick solution to get the post count any kind of filtering you want

function custom_get_user_posts_count($user_id,$args );  
    $args['author'] = $user;
    $args['fields'] = 'ids';
    $ps = get_posts($args);
    return count($ps);
}

Since this function uses get_posts you can filter and use anything that you can with WP_Query

So in your case you can use it like this:

$count = custom_get_user_posts_count($user_id, array(
    'post_type' =>'post',
    'post_status'=> 'draft'
));

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *