如何在bbPress中按创建主题数排序用户并列出TOP5?
Absolutely doable! You don’t need a dedicated plugin for this—with a bit of custom code tailored to WordPress and bbPress, you can easily pull and sort users by how many forum topics they’ve created. Let’s break this down.
Yes, 100%. bbPress stores forum topics as custom post types (topic) in your WordPress database, linked directly to the author’s user ID. This means we can leverage WordPress’s built-in functions or direct database queries to count topics per user and sort them accordingly.
Method 1: Use WP_User_Query (Recommended)
This approach follows WordPress best practices and avoids unnecessary direct database calls. Here’s a reusable function to fetch your top topic creators:
function get_top_topic_authors($limit = 5) { global $wpdb; // Join users with their published topic counts $user_query = new WP_User_Query(array( 'number' => $limit, 'orderby' => 'topic_count', 'order' => 'DESC', 'fields' => 'all_with_meta', 'join' => "LEFT JOIN ( SELECT post_author, COUNT(ID) AS topic_count FROM {$wpdb->posts} WHERE post_type = 'topic' AND post_status = 'publish' GROUP BY post_author ) AS topic_counts ON {$wpdb->users}.ID = topic_counts.post_author", 'where' => 'topic_counts.topic_count > 0', )); return $user_query->get_results(); }
Display the Top Authors
Add this code to your theme’s template file (like forum-index.php or a custom widget) to render the list:
$top_authors = get_top_topic_authors(5); if (!empty($top_authors)) { echo '<h3>Top Topic Creators</h3>'; echo '<ul class="top-authors-list">'; foreach ($top_authors as $author) { // Use bbPress's built-in function for accurate topic counts $topic_count = bbp_get_user_topic_count($author->ID); echo '<li>'; echo get_avatar($author->ID, 32); // Show user avatar echo '<a href="' . bbp_get_user_profile_url($author->ID) . '">' . esc_html($author->display_name) . '</a>'; echo '<span class="count">(' . $topic_count . ' topics)</span>'; echo '</li>'; } echo '</ul>'; }
Method 2: Create a Shortcode for Easy Placement
If you want to add this list to posts, pages, or widgets without editing template files, create a shortcode:
add_shortcode('top_topic_authors', 'display_top_topic_authors'); function display_top_topic_authors($atts) { // Set default shortcode attributes $atts = shortcode_atts(array( 'limit' => 5, ), $atts); $top_authors = get_top_topic_authors($atts['limit']); if (empty($top_authors)) return '<p>No topic creators found yet.</p>'; $output = '<div class="top-topic-authors">'; $output .= '<h3>Top ' . esc_html($atts['limit']) . ' Topic Creators</h3>'; $output .= '<ul>'; foreach ($top_authors as $author) { $topic_count = bbp_get_user_topic_count($author->ID); $output .= '<li>'; $output .= get_avatar($author->ID, 40); $output .= '<span class="author-name"><a href="' . esc_url(bbp_get_user_profile_url($author->ID)) . '">' . esc_html($author->display_name) . '</a></span>'; $output .= '<span class="topic-count">' . esc_html($topic_count) . ' topics</span>'; $output .= '</li>'; } $output .= '</ul></div>'; return $output; }
Now you can just drop [top_topic_authors limit="5"] anywhere on your site to show the list instantly.
Pro Tips for Accuracy & Performance
- Keep Counts Up-to-Date: bbPress usually updates the
bbp_topic_countuser meta automatically when topics are published/deleted. If counts look stale, run this one-time script to refresh them (add tofunctions.php, run once, then remove):function refresh_bbp_topic_counts() { $users = get_users(array('role__in' => array('bbp_participant', 'bbp_moderator', 'bbp_admin'))); foreach ($users as $user) { $count = bbp_get_user_topic_count_raw($user->ID); update_user_meta($user->ID, 'bbp_topic_count', $count); } } // Uncomment below to run once: // refresh_bbp_topic_counts(); - Optimize for Large Forums: The
WP_User_Querywith a custom join is more efficient than counting posts per user, especially if you have hundreds of users. Using the pre-calculatedbbp_topic_countmeta is even faster—just ensure it’s refreshed regularly.
This solution gives you full control over how you display the top authors, and it’s lightweight since it doesn’t rely on a third-party plugin.
内容的提问来源于stack exchange,提问作者Crookie Susoku

