0

I have this shortcode to display a loop of user avatars with order by registration date.

function Profiles() {
    $args = array(
    'orderby' => 'registered',
    'order' => 'DESC',
    'fields' => 'all_with_meta',
    );
    $user_query = new WP_User_Query( $args );
    if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) { ?>

    <div class="user-item user-img-<?php echo $user->ID; ?>" rel="<?php echo $user->ID; ?>">
    <a href="<?php echo $website; ?>" rel="<?php echo $user->ID; ?>" target="_blank"><?php echo get_avatar( $user->ID, 100 ); ?></a>    
    </div>
    <?php }
    } else {
        echo 'No users found.';
    }
}
add_shortcode('profiles', 'Profiles');

If any user role changes to sticky (a custom role) then that users move to top of the list. Default orderby should stay as registered for all other users in the loop. Once sticky user role is removed, it goes back to normal sorting.

I gave it quite a bit thought and maybe if i can combine two arguments in a single Wp_User_Query that might do the trick?

For example $sticky_args have role__in => 'sticky' and $simple_args have all others roles => 'subscriber', 'customer', 'author' OR role__not_in => 'sticky' argument. Then combining this Wp_User_Query in a way that $user_query->results shows $sticky_args users list first and then $simple_args user list after that in the loop. I tried several codes and trying to think if statements but nothing works so far. Google didn't help either. Would appreciate the help.

2 Answers 2

0

If you read the code (in version 4.5.2 on line 164-173) you will see that orderby can be an array of values, coupled with order. So you could modify $args like this:

     $args = array(
        'orderby' => array(
            array ('sticky' => 'DESC'),
            array ('registered' => 'DESC')),
        'fields' => 'all_with_meta',
        );

Note that I know nothing about how woocommerce defines the sticky user role, so I don't know if this code will actually work. It only shows how to implement a multidimensional array in the query.

2
  • I'm using 4.4.3.. Need update wordpress to test this, have some theme competbility issues, so i will try it on a new installation. Just a question, that sticky in 'sticky' => 'DESC' will represent user role? I didn't knew we can get user role directly in orderby
    – Ali
    Jun 1, 2016 at 21:28
  • Please read the note at the end of my answer.
    – cjbj
    Jun 2, 2016 at 6:35
0

I got it resolved by adding 2 loops after each other. First loop display users with 'role__in' => 'sticky' while second loop display users with 'role__not_in' => 'sticky' It worked perfectly. Here is the code if it helps anyone.

function Profiles() {

    echo "<div class='users-list'>"

    // First loop with sticky users
    $args = array(
    'role__in' => 'sticky',
    'orderby' => 'registered',
    'order' => 'DESC',
    'fields' => 'all_with_meta',
    );
    $user_query = new WP_User_Query( $args );
    if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) { ?>

    <div class="user sticky-user user-img-<?php echo $user->ID; ?>" rel="<?php echo $user->ID; ?>">
    <a href="<?php echo $website; ?>" rel="<?php echo $user->ID; ?>" target="_blank"><?php echo get_avatar( $user->ID, 100 ); ?></a>    
    </div>
    <?php }
       }

    // Second loop without sticky users
    $args = array(
    'role__not_in' => 'sticky',
    'orderby' => 'registered',
    'order' => 'DESC',
    'fields' => 'all_with_meta',
    );
    $user_query = new WP_User_Query( $args );
    if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) { ?>

    <div class="user user-img-<?php echo $user->ID; ?>" rel="<?php echo $user->ID; ?>">
    <a href="<?php echo $website; ?>" rel="<?php echo $user->ID; ?>" target="_blank"><?php echo get_avatar( $user->ID, 100 ); ?></a>    
    </div>
    <?php }
    } else {
        echo 'No users found.';
    }

    echo "</div>"
}
add_shortcode('profiles', 'Profiles');

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.