Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the following function to count the number of posts an author has made from a custom post type.

<?php
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

2 Answers 2

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 = "
    SELECT COUNT(*)  
    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

 
discard

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.