1

I had this function that counts posts number by type (taken from wordpress codex)

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);
}

and I changed it in order to display even posts with "draft" and "pending" status.

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 
                            AND (post_status = 'publish' OR post_status = 'draft' OR post_status = 'pending')");
    return apply_filters('get_usernumposts', $count, $userid);
}

but it doesn't work. If I echo the function like this

echo count_user_posts_by_type(wp_get_current_user()->ID, 'percorso');

I can see it counts just published posts. What's wrong with what I wrote? Thanks!

EDIT: as suggested, I performed a var_dump for SELECT COUNT etc. here's what I got

string(216) 
    "SELECT COUNT(*) FROM mi_posts 
    WHERE post_author = 1 
    AND post_type = 'percorso' 
    AND (post_status = 'publish' OR post_status = 'private') 
    AND (post_status = 'publish' OR post_status = 'draft' OR post_status = 'pending')"
5
  • I don't see anything wrong with it. Post the generated SQL and see if it works without that apply_filters part.
    – s_ha_dum
    Commented Jan 19, 2013 at 17:28
  • where can I find the generated sql? what do you mean? Sorry, I'm just a newbie :P anyway, I tried to delete the 'apply_filters' part but the function doesn't work at all without that piece of code.
    – She Hulk
    Commented Jan 19, 2013 at 19:56
  • Add var_dump("SELECT ... post_status = 'pending'"); to your code. If you delete that filter line you have to return $count; I am more interested in seeing what the output is than in having a working piece of code, right now.
    – s_ha_dum
    Commented Jan 19, 2013 at 20:02
  • Maybe it can be done without $wpdb...
    – brasofilo
    Commented Jan 19, 2013 at 20:47
  • s_ha_dum I edited my original post and added the var_dump. thanks!
    – She Hulk
    Commented Jan 20, 2013 at 13:52

1 Answer 1

0

It's not necessary to use $wpdb, a simple get_posts can handle it. Check WP_Query for the full list of parameters.

function count_user_posts_by_type( $userid, $post_type ) 
{
    $args = array(
        'numberposts'   => -1,
        'post_type'     => $post_type,
        'post_status'   => array( 'publish', 'private', 'draft', 'pending' ),
        'author'        => $userid
    );
    $count_posts = count( get_posts( $args ) ); 
    return $count_posts;
}
3
  • Thanks brasofilo! Your function is almost perfect, but it has two little mistakes. I edited it and successfully used it on my blog. Can I edit your post? Or it's better if I add a new answer to the post with the correct version?
    – She Hulk
    Commented Feb 16, 2013 at 12:49
  • @SheHulk, what mistakes?
    – brasofilo
    Commented Feb 16, 2013 at 16:33
  • Sorry, I read my question and realized you wrote it like this to exactly display publish, drafts and pending posts as I asked, but when I tryed to use it in my template it didn't work (I don't understand why, I assumed there was a mistake in the function but more likely the mistake was in how I used it :P ). So I changed 'post_status' => array( ), with post_status' => $post_status, and added $post_status parameter to the function. Then simply called it different times in the template (count drafts, count published, etc.) and added together the values, when needed.
    – She Hulk
    Commented Feb 16, 2013 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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