Browse: Home / Snippets /

Get post author counts

Contents


Snippet #

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;
}