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.

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!

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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'));
share|improve this answer
    
I've checked the wordpress codex and it is not saying anything about author parameters, I know it might not be completed but my query also doesn't work(might be other reasons) but are you sure that get_posts accepts author $user as a parameter? –  alex alex Oct 8 '13 at 23:38

Here is a custom function I've cooked up, which allows you to query by post type and post status for a particular author. You may need to be careful what arguments you pass as it can include auto-drafts and revisions...

The following is adapted from the get_posts_by_author_sql

function my_count_posts_by_user($post_author=null,$post_type=array(),$post_status=array()) {
    global $wpdb;

    if(empty($post_author))
        return 0;

    $post_status = (array) $post_status;
    $post_type = (array) $post_type;

    $sql = $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = %d AND ", $post_author );

    //Post status
    if(!empty($post_status)){
        $argtype = array_fill(0, count($post_status), '%s');
        $where = "(post_status=".implode( " OR post_status=", $argtype).') AND ';
        $sql .= $wpdb->prepare($where,$post_status);
    }

    //Post type
    if(!empty($post_type)){
        $argtype = array_fill(0, count($post_type), '%s');
        $where = "(post_type=".implode( " OR post_type=", $argtype).') AND ';
        $sql .= $wpdb->prepare($where,$post_type);
    }

    $sql .='1=1';
    $count = $wpdb->get_var($sql);
    return $count;
} 
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.