I’ve been looking for a way to count the number of custom posts a user has created, and was able to do it using this snippet:
<?php
$userid = get_current_user_id();
function count_user_posts_by_type($userid, $post_type = 'foo_type', $post_status = 'publish') {
global $wpdb;
$query = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $userid AND post_type = '$post_type' AND post_status = '$post_status'";
$count = $wpdb->get_var($query);
return apply_filters('get_usernumposts', $count, $userid);
} ?>
And then echo the result with:
<?php echo count_user_posts_by_type($userid); ?>
My question: Above code outputs only the count of the custom post type “foo_type”. If I have two custom post types – “foo_type” and “bar_type” – how do I change this code so that it returns the count of both of them rather than just the count of “foo_type”?
Add the second post_type to the query:
Try custom function given below
And then echo the result with:
Please look on url given below..
https://wordpress.stackexchange.com/questions/43549/count-user-posts-by-user-id-post-type-and-post-status
thanks
You can pass any Arguments in $args which support query_posts