Im looking to order posts by the users role... I currently have 2 user roles (intending to create more), and they are "Premium User" and "User", and some poss created by "Admin"

All of these posts are on one page, and i would like all the posts that are written by the "Premium User" at the top, then the rest underneath.

I have currently accomplished this with 2 separate loops, however, as the site grows there will be need for pagination to be added, and with 2 separate loops, this isnt going to work.

Any help appreciated!


  • Here is a similar Question and Answer: wordpress.stackexchange.com/questions/20804/… Jul 3, 2012 at 15:00
  • 1
    Note that a user can have multiple roles, for example administrator and zenmaster. How should ordering work then?
    – fuxia
    Jul 3, 2012 at 16:24
  • I wasnt aware that I could assign the user to more than 1 role... For this website instance, they will only have 1 user role per user. Failing that, if need be: Any user with the role "Premium User" would appear first.
    – Mike
    Jul 4, 2012 at 10:34

1 Answer 1


This one's a little difficult, as there's no default user role meta key or post data. The only thing we got is the user ID.

// Get the posts
$posts_by_author_ID = get_posts( array(
     'post_type'   => 'post'
    ,'post_status' => 'publish'
    ,'orderby'     => 'post_author'
    ,'order'       => 'DESC'
) );
foreach ( $posts_by_author_ID as $post )
    static $user;

    $new_user = get_user_by( 'id', $post->post_author );

    // Let's save some queries
    if ( 
        isset ( $user ) 
        AND $user->ID === $new_user->ID

    $user = $new_user;

    foreach ( $user->roles as $role )
        $posts_by_author_role[ $role ] = array( 
             'user' => $user
            ,'post' => $post
// Sort by key a.k.a. role
ksort( $posts_by_author_ID );

echo '<pre>'; // Only for debugging/developing - should get deleted when done
foreach ( $posts_by_author_ID as $role => $data )
    list( $user, $post ) = $data;
    // Now we can output our data
    // The user data is saved inside $user
    // The actual post data is saved inside $data

    // The following is only for debugging/developing - should get deleted when done
    var_export( $user, false );
    var_export( $post, false );
echo '</pre>'; // Only for debugging/developing - should get deleted when done
  • Couldnt get this to work, or even display posts? I think i edited parts wrong maybe... Custom Post Type is called: Model 1st Role: Premium Models 2nd/3rd Role: Administrator / Model (no preference for ordering these Potentially another one will be adde din future called "Super Premium Model" Thanks,
    – Mike
    Jul 4, 2012 at 10:25
  • @Mike You need the internal name as @toscho pointed out in the comments to your question above ↑. This means it's not Premium Models, but something without spaces and lowercased like (for e.g.) premium-models. You should check this and use this user role. Also: Dump the $posts_by_author_ID to see if this part works, then we're proceeding further.
    – kaiser
    Jul 4, 2012 at 10:48
  • Hopefully this helps (it could be something else ive done wrong: pastebin.com/HNx6WkeD Thanks
    – Mike
    Jul 4, 2012 at 10:59
  • @Mike I asked you to dump the result. Not showing my code inside your template (helps me nothing, as I don't have your setup + DB). Here's a pastebin with debugging code. For the rest, you'll have to dig into it, learn some basic PHP to know what you're doing and then edit the/my answer above with what worked (without the actual surrounding template stuff).
    – kaiser
    Jul 4, 2012 at 11:14
  • Sorry, really struggling with this... Its only outputting the Page, not the Posts in the Model custom post category, result here: www.lauralee-world.co.uk/ (NSFW)
    – Mike
    Jul 4, 2012 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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