1

Currently I'm trying to create a author list with all authors but on top some 'special' authors. The 'special' authors should result first and shouldn't be available in the list with normal authors.

The 'special' authors have in usermeta a meta key called 'werksortgodmodus' with meta value 'yes'. The 'rest' of the authors have 'werksortgodmodus' = no .

The result should be something like this:

[author with godmodus], [author with godmodus], [author with godmodus], [normal author], [normal author], [normal author].

This is the code I found and stripped to get authors. How can I get authors with godmodus on top?

[edit] replaced code with original code [/edit]

    <?php
    $display_admins = false;
    $order_by = 'display_name'; 
    $role = 'author';
    $avatar_size = 64;

if(!empty($display_admins)) {
    $blogusers = get_users('orderby='.$order_by.'&role='.$role);
} else {
    $admins = get_users('role=administrator');
    $exclude = array();
    foreach($admins as $ad) {
        $exclude[] = $ad->ID;
    }
    $exclude = implode(',', $exclude);
    $blogusers = get_users('exclude='.$exclude.'&orderby='.$order_by.'&role='.$role);
}
$authors = array();
foreach ($blogusers as $bloguser) {
    $user = get_userdata($bloguser->ID);
    if(!empty($hide_empty)) {
        $numposts = count_user_posts($user->ID);
        if($numposts < 1) continue;
    }
    $authors[] = (array) $user;
}        
    echo '<ul class="contributors">';
    foreach($authors as $author) {
        $display_name = $author['display_name'];
        $description = get_userdata($author['ID'])->user_description;
        $avatar = get_avatar($author['ID'], $avatar_size);
        $author_profile_url = get_author_posts_url($author['ID']);

        echo '<li><h3>'.$display_name.'</h3><a href="', $author_profile_url, '">', $avatar , '</a><p>'.$description.'</p><p><a href="', $author_profile_url, '" class="contributor-link">➤ Posts by '.$display_name.'</a></p></li>';
    }
    echo '</ul>';
    ?>
3

The 'special' authors have in usermeta a meta key called 'werksortgodmodus' with meta value 'yes'. The 'rest' of the authors have 'werksortgodmodus' = no .

You could e.g. order by the meta value with (PHP versions 5.4+):

$blogusers = get_users( 
    [ 
        'role'     => 'author',
        'meta_key' => 'werksortgodmodus', 
        'orderby' => [ 'meta_value' => 'DESC', 'display_name' => 'ASC' ], 
    ] 
);

where we order first by the werksortgodmodus meta value and then by e.g. display_name.

We use the DESC order so that the yes values come before the no values.

If you got huge number of users, then you might want to use query pagination.

  • As of PHP 5.4 you can also use the short array syntax, which replaces array() with []. - not everyone has got PHP 5.4+ so you should change [] to array() (source) – Krzysztof Grabania Aug 17 '16 at 9:21
  • Thanks for notice, I usually add the notice to my snippets, when I remember. But this is also to promote visitors to avoid the old PHP versions. I stick to the short array notation ;-) @KrzysztofGrabania – birgire Aug 17 '16 at 9:25
  • This looks what I'm looking for, but how do I embed it to my current code? Sorry I'm still basic PHP.. – rwzdoorn Aug 17 '16 at 9:45
  • How do you fetch data into $blogusers? Please add that to the question. @rwzdoorn – birgire Aug 17 '16 at 9:47
  • Added the missing code from original code! – rwzdoorn Aug 17 '16 at 10:15

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.