September 27, 2012 / IST / Web Development.

Here i share wordpress php code to display all author with post count in dropdown.
Step 1: First copy and paste code into function.php.

function wp_dropdown_authors($args = '') {
	global $wpdb;

	$defaults = array(
		'optioncount' => false, 'exclude_admin' => true,
		'show_fullname' => false, 'hide_empty' => true,
		'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true
	);

	$r = wp_parse_args( $args, $defaults );
	extract($r, EXTR_SKIP);

	/** @todo Move select to get_authors(). */
	$return="<select name=author-db-list id=author-db-list selected=0>\n";
	$return .="\t<option value=0>Select Author</option>";
	$option="";
	$authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users " . ($exclude_admin ? "WHERE user_login <> 'admin' " : '') . "ORDER BY display_name");

	$author_count = array();
	foreach ((array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row) {
		$author_count[$row->post_author] = $row->count;
	}
	foreach ( (array) $authors as $author ) {
		$author = get_userdata( $author->ID );
		$posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
		$name = "$author->first_name $author->last_name";
		$author_id= "$author->ID";

		if (($posts==0 && $hide_empty)){
			continue;
		}else{
			$option .="\t<option value=$author_id>$name ($posts)</option>\n";
		}
	}
	$return .=$option;
	$return .= "</select>\n";
	echo $return;
}

Step 2: Now call a function wp_dropdown_authors(); anywhere in your blog to display author.

Thanks :)

  • manfergo

    Hi, how could I go to the author page making click in the dropdown name?.

    Thanks.

    View Comment