Hello i m a newbie in php and Wordpress development.
I was struggling to get a user custom post count in my wordpress/buddypress site frontend.
i m using this code:
// add [author-post-count] shortcode to your frontend
add_shortcode('author-posts-count', 'count_user_posts_function');
function count_user_posts_function ($userid, $post_type ) {
$user_id = get_current_user_id();
$totalUser =count_user_posts( $user_id, $post_type = 'posts_comcurso' );
return $totalUser;
}
i m adding [author-posts-count] in my frontend.
This way i manage to show user custom post count.
If anyone has a simple or better way please let me know.
Hello i m a newbie in php and Wordpress development.
I was struggling to get a user custom post count in my wordpress/buddypress site frontend.
i m using this code:
// add [author-post-count] shortcode to your frontend
add_shortcode('author-posts-count', 'count_user_posts_function');
function count_user_posts_function ($userid, $post_type ) {
$user_id = get_current_user_id();
$totalUser =count_user_posts( $user_id, $post_type = 'posts_comcurso' );
return $totalUser;
}
i m adding [author-posts-count] in my frontend.
This way i manage to show user custom post count.
If anyone has a simple or better way please let me know.
Share Improve this question edited Aug 13, 2020 at 10:19 pedro davim asked Aug 13, 2020 at 8:56 pedro davimpedro davim 34 bronze badges 01 Answer
Reset to default 0If you were to use WP_Query to retrieve a list of posts, you can then access the number of posts using 'found_posts'.
e.g. (from the support pages)
$query = new WP_Query( array( 'author' => 123, 'post_type' => 'posts_comcurso' ) );
Would return all posts with a userid of 123 in your selected post type. Then access the number of posts like so:
$count = $query->found_posts;
There may be other ways but the above gives you a lot of flexibility to target the posts you want to count.
Here's the link to WP_Query support page