How to fetch the post count based on usernames for all users in WordPress

Code attaching below:

function get_author_post_count() {
    $authors = get_users( array( 'role' => array('Subscriber') ) );
    $author_post_count = array();
    
    foreach ( $authors as $author ) {
        $args = array(
            'author'         => $author->ID,
            'posts_per_page' => -1,
        );
        
        $author_posts = get_posts( $args );
        $author_post_count[ $author->display_name ] = count( $author_posts );
    }
    
    return $author_post_count;
}

$author_post_count = get_author_post_count();
foreach ( $author_post_count as $author_name => $post_count ) {	
			 echo $author_name;
			 echo $post_count;
	  	
		}	
	

Here, the post counts that are fetching for the user who has the role ‘Subscriber’.

Leave a comment

Your email address will not be published. Required fields are marked *