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 using the following function to count the number of posts an author has made from a custom post type.

function count_user_posts_by_type($userid, $post_type) {
    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);

To echo out the count I use

<?php echo count_user_posts_by_type(wp_get_current_user()->ID, 'custom_post_type_name'); ?>

From my testing it seems to count just the posts that have a post_status of publish. How can I modify the function so it also counts posts that are draft ?

I've tried to modify the query as follows but it seems to have no effect whatsoever:

$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where AND post_status IN ('publish','draft')" );
share|improve this question

You can use AND and OR statements in MySQL. Try this:

$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where AND (post_status = 'publish' OR post_status = 'draft')" );
share|improve this answer

You can try something like this:

$querystr = "
    FROM $wpdb->posts
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
    AND $wpdb->posts.post_status = 'draft' 
    AND $wpdb->posts.post_type = 'post'
    ORDER BY $wpdb->posts.post_date DESC

$pageposts = $wpdb->get_results($querystr, OBJECT);

Just modify the query to retrieve the posts for the current user.

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.