Wordpress - How to Display a List of Users Who Have Made at Least 1 Post?
You need to set the who
parameter in get_users
<?php
$blogusers = get_users( 'orderby=post_count&who=authors' );
foreach ( $blogusers as $user ) {
echo '<li>' . esc_html( $user->display_name ) . '</li>';
}
?>
EDIT
Seems I was to fast answering. The code in your question and in my answer is the start to what you want to achieve.
I don't have time to code now, off to watch rugby, but here is the complete code used in the twenty fourteen to display authors and their post count. Hope this helps
function twentyfourteen_list_authors() {
$contributor_ids = get_users( array(
'fields' => 'ID',
'orderby' => 'post_count',
'order' => 'DESC',
'who' => 'authors',
) );
foreach ( $contributor_ids as $contributor_id ) :
$post_count = count_user_posts( $contributor_id );
// Move on if user has not published a post (yet).
if ( ! $post_count ) {
continue;
}
?>
<div class="contributor">
<div class="contributor-info">
<div class="contributor-avatar"><?php echo get_avatar( $contributor_id, 132 ); ?></div>
<div class="contributor-summary">
<h2 class="contributor-name"><?php echo get_the_author_meta( 'display_name', $contributor_id ); ?></h2>
<p class="contributor-bio">
<?php echo get_the_author_meta( 'description', $contributor_id ); ?>
</p>
<a class="button contributor-posts-link" href="<?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?>">
<?php printf( _n( '%d Article', '%d Articles', $post_count, 'twentyfourteen' ), $post_count ); ?>
</a>
</div><!-- .contributor-summary -->
</div><!-- .contributor-info -->
</div><!-- .contributor -->
<?php
endforeach;
}
Simply call it in your template files as
twentyfourteen_list_authors();
There is no default way in WordPress to do this task, as Pieter Goosen pointed out, exists the argument who
for get_users()
that returns users that can post, not the users that have posted.
However, you can use 'pre_user_query'
to add a JOIN
SQL clause to only get users that have at least one post.
To be honest, when you query users ordering them by post count, the join is already created by WordPress, but using an OUTER LEFT JOIN
, so even users with no posts are returned, so the only thing you need is to replace the OUTER LEFT JOIN
to a INNER JOIN
function filter_users_have_posted( $user_query ) {
$user_query->query_from = str_replace( 'LEFT OUTER', 'INNER', $user_query->query_from );
remove_action( current_filter(), __FUNCTION__ );
}
add_action( 'pre_user_query', 'filter_users_have_posted' );
$blogusers = get_users( 'orderby=post_count&order=desc' );
Since version 4.3.0
, you can now specify the parameter has_published_posts
to the get_users();
function call.
Pass an
array
of post types to filter results to users who have published posts in those post types.true
is an alias for all public post types.
Example
if ( $users = get_users( array(
'orderby' => 'nicename',
'order' => 'ASC',
'has_published_posts' => array( 'post' ),
'blog_id' => absint( get_current_blog_id() )
) ) ) {
print_r( $users );
}
Resources
- https://codex.wordpress.org/Function_Reference/get_users
- https://developer.wordpress.org/reference/functions/get_users/
- https://developer.wordpress.org/reference/classes/wp_user_query/