0

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.

0

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

| improve this answer | |
  • Thanks for the help. – pedro davim Aug 13 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.