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')"
It’s not necessary to use
$wpdb
, a simpleget_posts
can handle it. CheckWP_Query
for the full list of parameters.