2

I would like to retrieve a list of 12 Wordpress users, but I would like to have them randomly (at each page reload) AND only 12 users that have a Gravatar specified.

Right now, I have my 12 random users, but I can't only retrieve those with a Gravatar... Could you help me ?

Here is what I have right now :

Functions.php :

// ***********************************************************************************
// ******* Random users
// ***********************************************************************************
add_action( 'pre_user_query', 'my_random_user_query' );

function my_random_user_query( $class ) {
    if( 'rand' == $class->query_vars['orderby'] )
        $class->query_orderby = str_replace( 'user_login', 'RAND()', $class->query_orderby );

    return $class;
}

In my template :

<?php $args = array(
    'role'         => '',
    'meta_key'     => '',
    'meta_value'   => '',
    'meta_compare' => '',
    'meta_query'   => array(),
    'include'      => array(),
    'exclude'      => array(),
    'orderby'        => 'rand',
    'offset'       => '',
    'search'       => '',
    'number'       => '12',
    'count_total'  => false,
    'fields'       => 'all',
    'who'          => ''
  );

  $users = get_users($args);  
  ?>

    <ul class="you-know-people cf">
      <?php $i=0; ?>
        <?php foreach ( $users as $user ) { ?>

          <?php $author_avatar = get_avatar($user->ID,55); ?>

                <li class="you-know-people-item <?php if($i%4 === 0){echo 'first';}?>">
                    <a href="#">
                    <?= $author_avatar; ?>
                </a>
            </li>

            <?php $i++; ?>
        <?php }?>
    </ul>

Thank you in advance, Cédric

1

Gravatar by design doesn't require the downstream system to be aware if there is a match. The image (if any) is determined and served on request, only hash needs to be provided.

It's impossible to "guess" from email or its hash if it has an avatar. That can only be resolved by request to the Gravatar.

Basically to achieve this you will need to:

  1. query Gravatar service for each user
  2. store that information to query by
  3. oh and re-run this forever to keep it updated

This just isn't very practical and shoots the convenience that is the main point of Gravatar service.

The most common workaround is to use generated fallbacks (identicons, etc).

1
  • You could equally do it on demand: fetch twice as many random users as you need, then for each one query Gravatar until you find twelve hits, caching yes/no by user as you go for some sensible period. If you don't get 12 then either take the ones you do and fill in the rest from the non-Gravatar set, or make up to one extra request for another 24 random users to test. If your site was busy enough then the has-Gravatar status for all of your users should get cached relatively quickly and not cause any extra load. – Rup Aug 4 '15 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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