cc licensed flickr photo shared by bitzcelt
Elwood: It’s 106 miles to get this sidebar coded, we’ve got a full tank of gas, half a pack of cigarettes, it’s dark and we’re wearing sunglasses.
Jake: Hit it.
I have no idea why I opened this way except for Jim Groom Inspiration.
But to jive my code chops, there’s nothing more energizing than doing a little hack and chop coding in WordPress.
Today I was again doing some sidebar fine tuning on the NMC MIDEA web site (previously covered in my almost done series on doing custom post types in WordPress 3.0).
It was one of those “oh this might take 20 minute” deals that ended up going a bit longer. but like Jake and Elwood, when you gotta go to Chicago, you drop the sunglasses and hit it.
And this is pretty simple, and most likely there is a plugin that already does what I needed, but sometimes, it is just worth the effort to roll your own code. It’s the same joy of actually doing something in your car engine that actually does not result in flames or calling a tow truck.
What we have on this site is a number of accounts, me as admin, some staff as editors, and a few guest bloggers, with roles as authors. The category our gues authors blog under is called “Ideas“, and my idea was to add to the template sidebar, above the part where widgetized stuff happens, a blurb and a listing of our guest bloggers. I also liked the feature of not listing authors who have yet to post.
At first it seemed like the function wp_list_authors would do, but it has no way to list just authors of a certain role- it is meant for all authors (you can hide the administrator; I thought by making all our staff admins it would work, but that did not).
I scoured a few plugins, but most are widget oriented (its a long explanation, but because this only appears on category archives, doing a special set of widgets was overload) or or geared for listing author avatars.
Next I googled and found a custom function from WP Engineer that at first looked good, but it was written for previous versions of WordPress- things have really changed from the early days of basing author capabilities on an integer user role number, though the info is still there in the database. I started editing their script to get it to work, going back and forth to phpMyAdmin to run test queries.
it ended up sort of working, but still was not what I sought. I wanted it (a) to not only list the authors, but link to their profiles; (b) list the number of posts the author has written like the wp_list_authors() function does; (c) skip authors that have not posted (again like wp_list_authors() function does); and (d) list the authors in alphabetical order of their last name… which I did not find anywhere.
With some more elbow grease I more or less got it going, but really though the code was not quite as lean as it should be (this is the place where it may not pay to keep coding, but now Jake and Elwood were only 50 miles from the Windy City, and they could see the glow of light on the horizon).
There is actually quite a bit of fine grained tuning you can do for blog user accounts, down to the specific capabilities, and you can add/remove what accounts can do beyond the basic titles of “author” editor (see Roles and Capabilities). Most of this seems put to use for creators of plugins.
But with another round of Googling, I found the function I was looking for from Steve Taylor– it would return a list of ids of users that had a given role, and it made my code a lot shorter.
Only 25 miles til Chi-Town, boys….
I still ended up rolling some bits to do the parts I wanted… so here are the two functions I added to my functions.php template to generate a list of users with a given role as a >ul<…>/ul< list, in alphabetical order by last name (I made all the accounts, so I know they have these parts completed), skipping users with no posts…
It is pretty heavily commented, and ought to be semi-explanatory…
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
function getUsersByRole( $role ) { // find all users with given role // via http://sltaylor.co.uk/blog/get-wordpress-users-by-role/ if ( class_exists( 'WP_User_Search' ) ) { $wp_user_search = new WP_User_Search( '', '', $role ); $userIDs = $wp_user_search->get_results(); } else { global $wpdb; $userIDs = $wpdb->get_col(' SELECT ID FROM '.$wpdb->users.' INNER JOIN '.$wpdb->usermeta.' ON '.$wpdb->users.'.ID = '.$wpdb->usermeta.'.user_id WHERE '.$wpdb->usermeta.'.meta_key = \''.$wpdb->prefix.'capabilities\' AND '.$wpdb->usermeta.'.meta_value LIKE \'%"'.$role.'"%\' '); } return $userIDs; } function midea_list_authors($user_role='author', $show_fullname = true) { // Generate a list of authors for a given role // default is to list authors and show full name global $wpdb; $blog_url = get_bloginfo('url'); // store base URL of blog $holding_pen = array(); // this is cheap, a holder for author data echo '<ul>'; // get array of all author ids for a role $authors = getUsersByRole( $user_role ); foreach ( $authors as $item ) { // get number of posts by this author; custom query $post_count = $wpdb->get_results("SELECT COUNT( * ) as cnt FROM $wpdb->posts WHERE post_author =" . $item . " AND post_type = 'post' AND post_status = 'publish'"); // only output authors with posts; ugly way to get to the result, but it works.... if ($post_count[0]->cnt) { // load info on this user $author = get_userdata( $item); // store output in temp array; we use last names as an index in this array $holding_pen[$author->last_name] = '<li><a href="' . $blog_url . '/author/' . $author->user_login . '"> ' . $author->display_name . ' (' . $post_count[0]->cnt . ')</a> </li>'; } } // now sort the array on the index to get alpha order ksort($holding_pen); // now we can spit the output out. foreach ($holding_pen as $key=>$value) { echo $value; } echo '</ul>'; } |
And this is simply called in my main sidebar.php template… I use a conditional to insert it where I want, so it only appears on the archives for the “ideas” category (I made the defaults of my function match my use case… sue me!)
1 2 3 4 5 6 7 |
<?php if (is_category('ideas')) : ?> <h3>MIDEA Ideas</h3> <p>Our panel of guest bloggers, all of them experts in the museum and technology field, bring you food for thought. </p> <?php midea_list_authors(); ?> <?php endif?> |
And with that, welcome to the big city… see it in action
And the boys are now in town!
cc licensed flickr photo shared by cszar
Fantastic article and code. I used it to create an author plugin for my blog and it worked like a charm.
I changed it to work as a shortcode and made a few other improvements.
It is a good idea to not have the post count as part of the link. It looks nicer and is better for SEO. So I changed that.
Once the plugin is installed you can just add a shortcode to any post and it generates a list of authors. You can specify the role type in the short code like this:
[authors role=”contributor”]
Blog editor note… The code was sent to me via email.
You can find the sample code I used for my own web site at http://cogdogblog.com/code/show-authors.php.txt — you can update the plugin-code header to match your own site needs.
Thanks for this! 🙂 However, when I try the plugin, it seems to show only the first and last registered users of a particular role rather than the entire list. Am I missing something?
I did not code the plugin, but must admit, I did not try the “all” option. My code was set to only list them if they have anything published. I’ll see if I can check it out.
Hi, this all looks good, but wondering if you could suggest some tweaks to achieve what I’m after. I’d like to have one page that shows all authors with their user photo, bio and link to their individual author page, and another page that shows the same info for all contributors…
Its certainly possible if you have the PHP chops and WP template awareness. A custom authors list is a matter of editing the structure inside the loop if your authors.php template to get the content what you want, and formatted.
A contributors page? I would go about that by creating a page with the intro text you want, and then create a page template that loops through the users. You’d have to likely run a custom WP query to get the users with the role you want. The code above should be a good start.
If I spelled it out further, I’d be coding it!
Just used this to make a count of events that authors have submitted for the rvarts.org site through The Events Calendar plugin. The fun part is I ended up here through something else and I was like “I know that guy!”
Thanks.