I'm using the code below to try to get a proper post count of all posts by a user but it's returning 0 for each user.
function get_user_posts_count($user_id, $post_type) {
$args['author'] = $user_id;
$args['fields'] = 'ids';
$args['post_type'] = $post_type;
$args['fields'] = 'ids';
$args['blog_id'] = 4;
$args['post_status'] = 'any, trash, auto-draft';
$args['numberposts'] = -1;
$ps = get_posts($args);
return count($ps);
}
I'm using the code below to try to get a proper post count of all posts by a user but it's returning 0 for each user.
function get_user_posts_count($user_id, $post_type) {
$args['author'] = $user_id;
$args['fields'] = 'ids';
$args['post_type'] = $post_type;
$args['fields'] = 'ids';
$args['blog_id'] = 4;
$args['post_status'] = 'any, trash, auto-draft';
$args['numberposts'] = -1;
$ps = get_posts($args);
return count($ps);
}
The code you shared worked fine for me, so either your value for $user_id or $post_type is incorrect - or the user actually has no posts of the post type you're searching for.
function get_user_posts_count($user_id, $post_type) {
$args['author'] = $user_id;
$args['fields'] = 'ids';
$args['post_type'] = $post_type;
$args['fields'] = 'ids';
$args['blog_id'] = 4;
$args['post_status'] = 'any, trash, auto-draft';
$args['numberposts'] = -1;
$ps = get_posts($args);
return count($ps);
}
$user_id = get_current_user_id();
var_dump(get_user_posts_count( $user_id, 'post' ) );
// Output: 10
var_dump(get_user_posts_count( $user_id, 'page' ) );
// Output: 20
var_dump(get_user_posts_count( null, 'post' ) );
// Output: 0
var_dump(get_user_posts_count( $user_id, 'nonexistantposttype' ) );
// Output: 0