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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
If 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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0