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'm use the code below to get the number of posts by a user and it works fine but it only includes published posts. How can I modify this code or use different code to have it include "future" post_status's or "drafts", for example.

$post_count = count_user_posts($curauth->ID);
echo '<p>User post count is ' . $post_count;
share|improve this question
up vote 0 down vote accepted

This will do what you want:

$args = array(
    'author'      => $curauth->ID,
    'post_status' => array(
$posts = new WP_Query();

$post_count = $posts->found_posts;

It will include drafts, pending posts, published posts, and future posts. There are more you can set, such as trashed posts, and private posts, but that didn't seem to follow the intent of the question. This method will include custom post types as well as the builtin post types, so additional filtering may be in order.

Docs: WP_Query

share|improve this answer
Yeah. This is what I ended up doing that worked, based on what you said... $num = $postcount_query->post_count; echo $num; – Zach Reed Feb 6 '12 at 20:38
You probably want to use found_posts, post count can be affected by posts_per_page, which, while not used in this query, could present an issue down the line. Also, if you would, just tick the check under the answer so that anyone who comes by later can benefit from the knowledge you've gained. – mor7ifer Feb 6 '12 at 20:40
Done and done! Thanks! :) – Zach Reed Feb 6 '12 at 21:26

This can be done via a custom query: Be sure to hook the query filter early enough.

Note: This solution doesn't help more than the answer from @m0r7if3r above as the query will be run before the filter.

function count_all_posts_by_user_cb( $count, $userid )
    global $wpdb;

    $where = $wpdb->prepare( 'WHERE post_author = %d AND post_type = %s AND ', $userid, $wpdb->posts )
    $where .= "(post_status = 'publish'";
    // for a full list, @see http://codex.wordpress.org/Post_Status_Transitions
    foreach( array( 'private', 'draft', 'pending', 'future' ) as $status
        $where .= $wpdb->prepare( " OR post_status = '%s'", $status );
    $where .= ")";

    $result = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );

    return $result;
add_filter( 'get_usernumposts', 'count_all_posts_by_user_cb', 20, 2 );

Note: The filter will affect all calls to count_user_posts();. If you don't want that, then you'll have to check inside the filter cb fn, if the user has a specific role or ID or whatever restriction you need.

share|improve this answer

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.