Browse: Home / Snippets /

Get post author counts

Contents


Snippet #

Important: All snippets are provided as-is without support or guarantees. These snippets are provided as guidelines for advanced users looking to customize LearnDash. For any additional help or support with these snippets, we recommend reaching out to a LearnDash Expert.

function learndash_get_post_author_counts( $post_type = '', $post_status = 'publish', $post_author = 0 ) {
	$post_author_counts = array();
	
	if ( !empty( $post_type ) ) {
		$post_author_query_args = array(
			'post_type' 	=> $post_type,
			'post_status'	=> $post_status,
			'nopaging'		=> true
		);
		
		if ( !empty( $post_author ) ) {
			$post_author_query_args['author'] = intval( $post_author );
		}
		
		$post_author_query = new WP_Query( $post_author_query_args );
		if ( $post_author_query->have_posts() ) {
			foreach( $post_author_query->posts as $p ) {
				if ( !isset( $post_author_counts[$p->post_author] ) )
					$post_author_counts[$p->post_author] = 0;
				$post_author_counts[$p->post_author] += 1;
			}
		}
	}
	
	return $post_author_counts;
}