8

What I would like to do is have a list of users who have contributed at least one post.

I need to show the following:

[User photo] | [User Name] | [User post count]

e.g.

[photo] Joe Bloggs(8)

I made a start and went down this route:

<?php
   $blogusers = get_users( 'orderby=post_count' );
      foreach ( $blogusers as $user ) {
      echo '<li>' . esc_html( $user->display_name ) . '</li>';
   }
?>

However, this seems to just return all users registered to the blog rather than those who have contributed so I'm certainly not doing it correctly.

I am new to wordpress and PHP so all help would be appreciated.

1

3 Answers 3

8

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();
6
  • You could also add a status parameter codex.wordpress.org/Class_Reference/WP_Query#Status_Parameters Commented Aug 16, 2014 at 12:22
  • IIRC that display users that have capability to post, not user that have effectively posted something.
    – gmazzap
    Commented Aug 16, 2014 at 12:28
  • I'm referring to if have published posts if ( ! $post_count ) { continue; Commented Aug 16, 2014 at 12:32
  • Pieter, I think I can improve performance of your function, look at my answer ;)
    – gmazzap
    Commented Aug 16, 2014 at 13:30
  • count_many_users_posts() function should be used for efficiency, when counting for a set of multiple users.
    – Rarst
    Commented Jul 12, 2015 at 7:35
6

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' );
2
  • Must you burst my bubble, lol :-). Great answer, must say. Never thought in that direction to be honest. +1 Commented Aug 16, 2014 at 13:48
  • Thats great information to know! Thanks for your help. Just tried your solution and that also gets me close to where I need to be. :)
    – steakpi
    Commented Aug 16, 2014 at 13:48
4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.