Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Want something styled? Try this wpsites.net/web-design/author-contributors-page-template –  Brad Dalton Aug 16 at 12:24
    
Thanks Brad, I will have a look at that as well! –  steakpi Aug 16 at 13:12

2 Answers 2

up vote 4 down vote accepted

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();
share|improve this answer
    
You could also add a status parameter codex.wordpress.org/Class_Reference/WP_Query#Status_Parameters –  Brad Dalton Aug 16 at 12:22
    
IIRC that display users that have capability to post, not user that have effectively posted something. –  G. M. Aug 16 at 12:28
    
Thanks to all of you who have replied...I just tried the above code, and I think G.M has hit the nail on the head.. it has not returned users who have made posts. –  steakpi Aug 16 at 12:31
    
I'm referring to if have published posts if ( ! $post_count ) { continue; –  Brad Dalton Aug 16 at 12:32
    
Hey Brad, my PHP is very limited, how would I actually implement that into the code I posted? –  steakpi Aug 16 at 12:38

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' );
share|improve this answer
    
Must you burst my bubble, lol :-). Great answer, must say. Never thought in that direction to be honest. +1 –  Pieter Goosen Aug 16 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 Aug 16 at 13:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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