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.