Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top