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')" );

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')" );

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.