wp_list_authors()WP 1.2.0

Outputs a list of blog authors as links to their posts if posts exist.

You can also specify the output of the RSS feed link for each author and the number of posts for each author.

Returns

null|String. Nothing when echo = true. List of authors when echo = false.

Usage Template

$args = array(
	'orderby'       => 'name',
	'order'         => 'ASC',
	'number'        => null,
	'optioncount'   => false,
	'exclude_admin' => true,
	'show_fullname' => false,
	'hide_empty'    => true,
	'echo'          => true,
	'feed'          => '',
	'feed_image'    => '',
	'feed_type'     => '',
	'style'         => list,
	'html'          => ''
);

wp_list_authors( $args );

Usage

<?php wp_list_authors( $args ); ?>
$args(string|array)

Parameters responsible for output.

  • orderby(string)
    Field by which the result will be sorted. Since version 3.1. Available fields:

    • name - sort by display name (field display_name in the users table - by default).
    • email - by email address (field in the user_email table).
    • url - by URL address user_url.
    • registered - by field user_registered.
    • id - by user ID.
    • user_login - by user login (field user_login).
    • post_count - by the number of posts by the user.
      Default: 'name'
  • order(string)
    In what order to sort: ASC - in order, DESC - in reverse order. Since version 3.1.
    Default: 'ASC'

  • number(int)
    Maximum number of users to be shown (limit). Since version 3.1.
    Default: unlimited

  • optioncount(boolean)
    Show the number of posts for each user: 1 (true) - show, 0 (false) - do not show.
    Default: false

  • exclude_admin(boolean)
    Exclude the registration record with the login admin from the list: 1 (true) - exclude, 0 (false) - do not exclude (by default).
    Default: false

  • show_fullname(boolean)
    Show the full name (first name, last name) of the author; if set to false, the name chosen for display will be shown.
    Default: false

  • hide_empty(boolean)
    Do not show authors who have no posts: 1 (true) - show, 0 (false) - do not show.
    Default: true

  • echo(boolean)
    Output to the screen or return for processing: 1 (true) - output, 0 (false) - return.
    Default: true

  • feed(string)
    Text for the link to each author's feed; if no text is provided, the feed link will not be displayed.
    Default: ''

  • feed_image(string)
    Path/link to the image that will be shown as a link to each author's feed. Alternative to the feed parameter.
    Default: ''

  • feed_type(string)
    Type of feed. An empty string means the feed set on the blog by default; possible values: rss2, atom, rss, rdf.
    Default: ''

  • style(string)
    In what format to output the list. By default, it is output in the list format - ul list; if none is specified, authors will be shown separated by commas. This parameter works only if the html parameter is enabled.
    Default: 'list'

  • html(boolean)
    Enables or disables HTML formatting. By default, formatting is enabled (true); if disabled (false), the list will be output as links to authors separated by commas.
    Default: true

Default: empty string (predefined)

Examples

0

#1 Get a list of the authors of the site

The list displays the authors of the site, showing

  • the full name (show_fullname=1).
  • the number of posts of each author (optioncount=1).
  • Sorting is done in reverse order (order=DESC) by the number of posts of each author (orderby=post_count).
  • The output is limited to 3 authors (number=3).
  • Also, by default, the author with the nickname admin is excluded from the list.
  • the authors with no posts are excluded.
  • and the links to RSS feeds are not shown.
<?php
wp_list_authors( [
	'show_fullname' => 1,
	'optioncount'   => 1,
	'orderby'       => 'post_count',
	'order'         => 'DESC',
	'number'        => 3
] );
?>

The result is something like this:

Andrew Anderson (48)
Harriett Smith (42)
Sally Smith (29)

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 1.2.0 Introduced.

wp_list_authors() code WP 6.9.1

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

	$defaults = array(
		'orderby'       => 'name',
		'order'         => 'ASC',
		'number'        => '',
		'optioncount'   => false,
		'exclude_admin' => true,
		'show_fullname' => false,
		'hide_empty'    => true,
		'feed'          => '',
		'feed_image'    => '',
		'feed_type'     => '',
		'echo'          => true,
		'style'         => 'list',
		'html'          => true,
		'exclude'       => '',
		'include'       => '',
	);

	$parsed_args = wp_parse_args( $args, $defaults );

	$return = '';

	$query_args           = wp_array_slice_assoc( $parsed_args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
	$query_args['fields'] = 'ids';

	/**
	 * Filters the query arguments for the list of all authors of the site.
	 *
	 * @since 6.1.0
	 *
	 * @param array $query_args  The query arguments for get_users().
	 * @param array $parsed_args The arguments passed to wp_list_authors() combined with the defaults.
	 */
	$query_args = apply_filters( 'wp_list_authors_args', $query_args, $parsed_args );

	$authors     = get_users( $query_args );
	$post_counts = array();

	/**
	 * Filters whether to short-circuit performing the query for author post counts.
	 *
	 * @since 6.1.0
	 *
	 * @param int[]|false $post_counts Array of post counts, keyed by author ID.
	 * @param array       $parsed_args The arguments passed to wp_list_authors() combined with the defaults.
	 */
	$post_counts = apply_filters( 'pre_wp_list_authors_post_counts_query', false, $parsed_args );

	if ( ! is_array( $post_counts ) ) {
		$post_counts       = array();
		$post_counts_query = $wpdb->get_results(
			"SELECT DISTINCT post_author, COUNT(ID) AS count
			FROM $wpdb->posts
			WHERE " . get_private_posts_cap_sql( 'post' ) . '
			GROUP BY post_author'
		);

		foreach ( (array) $post_counts_query as $row ) {
			$post_counts[ $row->post_author ] = $row->count;
		}
	}

	foreach ( $authors as $author_id ) {
		$posts = isset( $post_counts[ $author_id ] ) ? $post_counts[ $author_id ] : 0;

		if ( ! $posts && $parsed_args['hide_empty'] ) {
			continue;
		}

		$author = get_userdata( $author_id );

		if ( $parsed_args['exclude_admin'] && 'admin' === $author->display_name ) {
			continue;
		}

		if ( $parsed_args['show_fullname'] && $author->first_name && $author->last_name ) {
			$name = sprintf(
				/* translators: 1: User's first name, 2: Last name. */
				_x( '%1$s %2$s', 'Display name based on first name and last name' ),
				$author->first_name,
				$author->last_name
			);
		} else {
			$name = $author->display_name;
		}

		if ( ! $parsed_args['html'] ) {
			$return .= $name . ', ';

			continue; // No need to go further to process HTML.
		}

		if ( 'list' === $parsed_args['style'] ) {
			$return .= '<li>';
		}

		$link = sprintf(
			'<a href="%1$s" title="%2$s">%3$s</a>',
			esc_url( get_author_posts_url( $author->ID, $author->user_nicename ) ),
			/* translators: %s: Author's display name. */
			esc_attr( sprintf( __( 'Posts by %s' ), $author->display_name ) ),
			$name
		);

		if ( ! empty( $parsed_args['feed_image'] ) || ! empty( $parsed_args['feed'] ) ) {
			$link .= ' ';
			if ( empty( $parsed_args['feed_image'] ) ) {
				$link .= '(';
			}

			$link .= '<a href="' . get_author_feed_link( $author->ID, $parsed_args['feed_type'] ) . '"';

			$alt = '';
			if ( ! empty( $parsed_args['feed'] ) ) {
				$alt  = ' alt="' . esc_attr( $parsed_args['feed'] ) . '"';
				$name = $parsed_args['feed'];
			}

			$link .= '>';

			if ( ! empty( $parsed_args['feed_image'] ) ) {
				$link .= '<img src="' . esc_url( $parsed_args['feed_image'] ) . '" style="border: none;"' . $alt . ' />';
			} else {
				$link .= $name;
			}

			$link .= '</a>';

			if ( empty( $parsed_args['feed_image'] ) ) {
				$link .= ')';
			}
		}

		if ( $parsed_args['optioncount'] ) {
			$link .= ' (' . $posts . ')';
		}

		$return .= $link;
		$return .= ( 'list' === $parsed_args['style'] ) ? '</li>' : ', ';
	}

	$return = rtrim( $return, ', ' );

	if ( $parsed_args['echo'] ) {
		echo $return;
	} else {
		return $return;
	}
}