Sometimes it’s necessary to manually select the order in which to sort your authors on your page listing all your authors or users. It is a little tricky to do, but I’ll break it down for you in this simple tutorial.
First, you are going to need to add a custom meta field for your users. There are many ways to do this that are not discussed here, though using Advanced Custom Fields is my favorite method, as the UI is all visual and requires no coding.
In this example, I created a custom meta field for authors named “order”. This is the field in which we are going to manually enter the order in which we want our authors to appear. In the user edit screen in the WordPress backend, setting our “order” value to ‘1’ should result in that author being listed first, with each subsequent number being listed after. I haven’t tested it to see if negative numbers would work, but I imagine they might!
Now to actually list the authors on our author results template. You need to have a separate template file for this. Typically to actually use it, you just create a page template and then select this template from the dropdown menu in the page edit screen.
The following code will query all the authors satisfying our parameters, and then sort them in our required order. I have a custom meta key which I use to specify which authors get listed, and it is simply a checkbox for the meta key named “display_author”. A meta value of “1” indicates that the box is checked and that other should be displayed. You could create this yourself, just like you created the “order” meta key if you wished. In my case, I have no other parameters, but you may want to add some, perhaps to specify that you only want “authors” as opposed to “subscribers” or any other role.
Now for the code. In this example, we are going to display the names of the authors with links to their profiles, a thumbnail, and a brief bio excerpt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php $args = array( 'meta_query' => array( array( 'key' => 'display_author', //My custom meta key to only display specific, handpicked authors 'value' => '1', ), ), ); $site_url = get_site_url(); //get the site url for later use $authors = get_users($args); // Get users satisfying our parameters function cmp($a, $b){ //The function to order our authors if ($a->order == $b->order) { //This is where the name of our custom meta key is entered, I named mine "order" return 0; } return ($b->order > $a->order ) ? -1 : 1; //The actual sorting is done here. Change ">" to "<" to reverse order } usort($authors, 'cmp'); //usort sorts our $users array with our function cmp() // Check for results if (!empty($authors)) //Only do this if there ARE any authors to list { foreach ($authors as $author) // loop trough each author { echo '<div>'; $author_info = get_userdata($author->ID); $link = '<a href="' . get_author_posts_url( $author->ID, $author->user_nicename ) . '" title="' . esc_attr( sprintf(__("Author Bio for %s"), $author->display_name) ) . '">'; // Create the link to their author page echo $link; echo get_avatar( $author->ID, 128 ); echo '</a>'; echo '<h2>' . $link . $author_info->first_name.' '.$author_info->last_name . ' ' . '-' . ' ' . get_user_meta($author->ID, 'title', true) .'</a></h2>'; echo '<p>'; echo word_count(get_user_meta($author->ID, 'description', true), '30'); //Limit bio excerpt to 30 words echo '...'; echo $link; echo 'View Full Profile</a></p></div><!-- .broker-results -->'; } } else { echo 'No authors found'; } ?> |
There you have it! This should be a good starting point to use for your own author page. Take a look at the comments to learn exactly how you can customize this.
One simple customization would to reverse the order of the authors which is actually quite simple. All you need to do is replace
1 |
return ($b->order > $a->order ) ? -1 : 1; |
with:
1 |
return ($b->order < $a->order ) ? -1 : 1; |
Simple! Let me know if you have any questions about this in the comments. Credit goes to ClarkLab for the idea and much of the original code, including the cmp function that actually does the sorting. As of 2017 the original post seems to be missing, but I’ll leave the link to his site up.
8 Comments on “Manual Sort Order for Authors or Users – WordPress”
I’m trying to make run this in a page but i have problems to see the users… Im trying to show all the authors and give a specific order to each one by the administrator via wordpress panel. With Advanced Custom Fields i made a function for all the authors, specifically i made an ‘number Field Type’ … but when i assign this values in your code on functions.php .. only shows me the massage ‘No authors found’ .. Can you help me.
Excellent Brian! Saved my bacon again
Excellent Brian thanks for sharing this.
This had me stumped until I came across your solution – which is actually quite simple. Love the way you’ve explained it and commented the code.
(There’s one tiny typo – 5th paragraph, 8th word, should be “authors” instead of “others”)
Thanks for taking the time to write this up and publish it. And leave it out there. I realize you did this years ago, but it’s still valuable today.
I’m glad you found it useful! While lots of things have changed in WordPress in the last few years, most of the core stuff still works 🙂
Oh, and thanks for pointing out the typo!
Thanks a lot for taking the time to explain how you did this. Helped me today.
Thank you sooooo much !
Thanks, Brian. Just what we needed!