hockeyman10 Posted July 20, 2010 Share Posted July 20, 2010 I was wondering if anyone could give me a hand. I haven't needed to go to a help site for quite some time lol. I'm having a bit of trouble with this code. It's to list all contributors on my Wordpress blog, but the code lists them by display_name, and I want it to sort by post count. The problem is that post count isn't a var in the authors[] array. How would I get $numposts into the array, and how could I call that in the sort function? Am I missing something really simple here?? Here is my code below: // Use this to sort authors by display name function sort_authors($a, $b) { return strcmp($a['display_name'], $b['display_name']); } // Output a list of contributors function contributors() { $authors = array(); // Get all blog users $blogusers = get_users_of_blog(); foreach ($blogusers as $bloguser) { // Fetch $user = get_userdata($bloguser->user_id); $numposts = get_usernumposts($user->ID); // Skip if admin or has no posts if ($user->user_login == 'admin' || $numposts < 1) continue; $authors[] = (array) $user; } // Sort the authors by display name usort($authors, 'sort_authors'); // Get the current page $paged = get_query_var('paged'); if (!$paged) $paged = 1; // We'll return these at a later stage $current_page = $paged; $total_pages = ceil(count($authors) / 10); // Calculate the starting and ending points // Assuming we'll be showing 10 authors per page $start = ($paged - 1) * 10; $end = $paged * 10; if ($end > count($authors)) $end = count($authors); // Loop through the authors for ($i = $start; $i < $end; $i++) { $author = $authors[$i]; // Gather data and meta $nicename = $author['user_nicename']; $display_name = $author['display_name']; $avatar = get_avatar($author['ID']); $author_profile_url = get_bloginfo('url') . '/author/' . $nicename . '/'; $website = get_usermeta($author['ID'], 'user_url'); $twitter = get_usermeta($author['ID'], 'twitter'); $bio = $author['description']; $numposts2 = $author['numposts1']; echo $numposts2; $numposts = get_usernumposts($author['ID']); // Hide if empty $twitter = strlen($twitter) > 0 ? 'Twitter: <a href="http://twitter.com/' . $twitter . '" target="_blank">@' . $twitter . '</a><br />' : ''; $website = strlen($website) > 0 ? 'Website: <a href="' . $website . '" target="_blank">Visit</a><br />' : ''; $bio = strlen($bio) > 0 ? 'Bio: ' . substr($bio, 0, 50) . '... <a href="' . $author_profile_url . '">Read more</a><br />' : ''; $this_count +=1; if ($this_count == "5") { print("<div style=\"clear: both;\"></div>"); $this_count = "0"; } // Output the list print <<<HTML <li> <a href="{$author_profile_url}">{$avatar}</a> <div id="authorname"> <center><a href="{$author_profile_url}" title="View {$display_name}'s Profile"> {$display_name}</a> <span class="postcount">($numposts)</span> </center> </div> </li> HTML; } // Return an array containing current and total pages // These values could be used to output pagination return array( 'current_page' => $current_page, 'total_pages' => $total_pages ); } print("<div style=\"clear: both;\"></div>"); I just need this to sort by the $numposts var, instead of by name. What am I missing here? Can this be done? Thanks a lot in advance to anyone that can give me a hand! Quote Link to comment https://forums.phpfreaks.com/topic/208328-at-this-for-4-hours-sort-users-by-post-count-instead-of-by-name/ Share on other sites More sharing options...
hockeyman10 Posted July 20, 2010 Author Share Posted July 20, 2010 Ok I finally got the var into the array as $authors[post_counter]. I have it sorting with two different codes, but it's sorting backwards. Does anyone know how I can reverse it? usort($authors, 'sorting_authors'); function sorting_authors($a, $b) { return strnatcmp($a['post_counter'], $b['post_counter']); } Quote Link to comment https://forums.phpfreaks.com/topic/208328-at-this-for-4-hours-sort-users-by-post-count-instead-of-by-name/#findComment-1088854 Share on other sites More sharing options...
hockeyman10 Posted July 21, 2010 Author Share Posted July 21, 2010 Figured it out...FINALLY. If anyone comes across this, all you need to do is reverse the order of $a and $b and it turns them around. Quote Link to comment https://forums.phpfreaks.com/topic/208328-at-this-for-4-hours-sort-users-by-post-count-instead-of-by-name/#findComment-1088877 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.