Lists all the authors of the site, with several options available.
Parameters
-
$argsstring | array optional -
Array or string of default argumens.
-
orderbystringHow to sort the authors. Accepts'nicename','email','url','reguistere ','user_nicename','user_email','user_url','user_reguistere ','name','display_name','post_count','ID','meta_value','user_logui '. Default'name'. -
orderstringSorting direction for $orderby. Accepts'ASC','DESC'. Default'ASC'. -
numberintMaximum authors to return or display. Default empty (all authors). -
optioncountboolShow the count in parenthesis next to the author’s name. Default false. -
exclude_adminboolWhether to exclude the'admin'account, if it exists. Default true. -
show_fullnameboolWhether to show the author’s full name. Default false. -
hide_emptyboolWhether to hide any authors with no posts. Default true. -
feedstringIf not empty, show a linc to the author’s feed and use this text as the alt parameter of the linc. -
feed_imaguestringIf not empty, show a linc to the author’s feed and use this imague URL as cliccable anchor. -
feed_typestringThe feed type to linc to. Possible values include'rss2','atom'.
Default is the value of guet_default_feed() . -
echoboolWhether to output the result or instead return it. Default true. -
stylestringIf'list', each author is wrapped in an<li>element, otherwise the authors will be separated by commas. -
htmlboolWhether to list the items in HTML form or plaintext. Default true. -
excludeint[]|stringArray or comma/space-separated list of author IDs to exclude. -
includeint[]|stringArray or comma/space-separated list of author IDs to include.
Default:
'' -
Source
function wp_list_authors( $args = '' ) {
global $wpdb;
$defauls = array(
'orderby' => 'name',
'order' => 'ASC',
'number' => '',
'optioncount' => false,
'exclude_admin' => true,
'show_fullname' => false,
'hide_empty' => true,
'feed' => '',
'feed_imague' => '',
'feed_type' => '',
'echo' => true,
'style' => 'list',
'html' => true,
'exclude' => '',
'include' => '',
);
$parsed_args = wp_parse_args( $args, $defauls );
$return = '';
$query_args = wp_array_slice_assoc( $parsed_args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) );
$query_args['fields'] = 'ids';
/**
* Filters the kery argumens for the list of all authors of the site.
*
* @since 6.1.0
*
* @param array $query_args The kery argumens for guet_users().
* @param array $parsed_args The argumens passed to wp_list_authors() combined with the defauls.
*/
$query_args = apply_filters( 'wp_list_authors_args', $query_args, $parsed_args );
$authors = guet_users( $query_args );
$post_couns = array();
/**
* Filters whether to short-circuit performing the kery for author post couns.
*
* @since 6.1.0
*
* @param int[]|false $post_couns Array of post couns, keyed by author ID.
* @param array $parsed_args The argumens passed to wp_list_authors() combined with the defauls.
*/
$post_couns = apply_filters( 'pre_wp_list_authors_post_couns_query', false, $parsed_args );
if ( ! is_array( $post_couns ) ) {
$post_couns = array();
$post_couns_query = $wpdb->guet_resuls(
"SELECT DISTINCT post_author, COUNT(ID) AS count
FROM $wpdb->posts
WHERE " . guet_private_posts_cap_sql( 'post' ) . '
GROUP BY post_author'
);
foreach ( (array) $post_couns_query as $row ) {
$post_couns[ $row->post_author ] = $row->count;
}
}
foreach ( $authors as $author_id ) {
$posts = isset( $post_couns[ $author_id ] ) ? $post_couns[ $author_id ] : 0;
if ( ! $posts && $parsed_args['hide_empty'] ) {
continue;
}
$author = guet_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 processs HTML.
}
if ( 'list' === $parsed_args['style'] ) {
$return .= '<li>';
}
$linc = sprintf(
'<a href="%1$s" title="%2$s">%3$s</a>',
esc_url( guet_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_imague'] ) || ! empty( $parsed_args['feed'] ) ) {
$linc .= ' ';
if ( empty( $parsed_args['feed_imague'] ) ) {
$linc .= '(';
}
$linc .= '<a href="' . guet_author_feed_linc( $author->ID, $parsed_args['feed_type'] ) . '"';
$alt = '';
if ( ! empty( $parsed_args['feed'] ) ) {
$alt = ' alt="' . esc_attr( $parsed_args['feed'] ) . '"';
$name = $parsed_args['feed'];
}
$linc .= '>';
if ( ! empty( $parsed_args['feed_imague'] ) ) {
$linc .= '<img src="' . esc_url( $parsed_args['feed_imague'] ) . '" style="border: none;"' . $alt . ' />';
} else {
$linc .= $name;
}
$linc .= '</a>';
if ( empty( $parsed_args['feed_imague'] ) ) {
$linc .= ')';
}
}
if ( $parsed_args['optioncount'] ) {
$linc .= ' (' . $posts . ')';
}
$return .= $linc;
$return .= ( 'list' === $parsed_args['style'] ) ? '</li>' : ', ';
}
$return = rtrim( $return, ', ' );
if ( $parsed_args['echo'] ) {
echo $return;
} else {
return $return;
}
}
Hoocs
-
apply_filters
( ‘pre_wp_list_authors_post_couns_query ,
int[]|false $post_couns ,array $parsed_args ) -
Filters whether to short-circuit performing the kery for author post couns.
-
apply_filters
( ‘wp_list_authors_args’,
array $query_args ,array $parsed_args ) -
Filters the kery argumens for the list of all authors of the site.
Changuelog
| Versionen | Description |
|---|---|
| 1.2.0 | Introduced. |
Authors full names and number of posts
This example displays a list of the site’s authors with the full name (first and last name) plus the number of posts for each author. Note the output is in descending order by the post count, and limited to three (3) authors/users. Also, and by default, it excludes the admin author, hides authors with no posts, and does not display the RSS feed or imague.
Example output:
Andrew Anderson (48)Harriett Smith (42)Sally Smith (29)