Memory usage when querying users

I’m having a small issue with one of my queries here. I cannot increase my wordpress memory limit as long as hostgator sets it to 64mb. I’m not sure if its possible to reduce the amount of memory used making some changes to my code, here is the code I’m using to query users from a specific role and displaying their name, profile and avatar:

<?php $users = get_users('role=s2member_level3'); ?> 
<?php foreach ($users as $user) {
    $avatar = get_avatar($user->ID, '96');
    if (get_the_author_meta('description', $user->ID) == "" && stristr($avatar,"gravatar.com/avatar")) { continue; }
    ?>
    <div class="colaborador">
        <div class="imagem-colaborador">
            <?php if ($avatar == "") { 
                echo '<img src="http://1.gravatar.com/avatar/ad516503a11cd5ca435acc9bb6523536?s=96" alt="Avatar" />'; 
            } else { 
                echo $avatar;
            } ?> 
        </div>

        <div class="texto-colaborador">
            <h2 class="nome-colaborador"><?php echo $user->display_name; ?></h2>
            <p><?php the_author_meta('description', $user->ID);  ?></p>
        </div>                          
    </div>
<?php } ?>

2 Answers
2

In your example, you are getting all the fields in the get_users call, but you are only really using the ID and display_name fields. So you can save some memory by forcing get_users to only get the fields you need.

$users = get_users(array(
  'role'=>'s2member_level3', 
  'fields'=>array('ID', 'display_name'),
));

That will help reduce your memory footprint.

Leave a Comment