Skip to:
Content
Pages
Categories
Search
Top
Bottom

Sort forums by number of topics in each forum

  • I’m trying to sort the display of the forums depending on the number of topics within the forum. Could somebody help me with a query for this? I’ve tried using a custom query but just cant seem to get it right.

    I know there’s a PHP function bbp_forum_topic_count() but I need this all within one loop, not PHP.

    Any help would be much appreciated.

    This was how far I got with my custom query:

    SELECT SQL_CALC_FOUND_ROWS wp_posts.*, topics.total FROM wp_posts, (SELECT COUNT(wp_posts.ID) as total FROM wp_posts) topics WHERE 1=1 AND wp_posts.post_type = ‘forum’ AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_status = ‘hidden’ OR wp_posts.post_status = ‘private’)

Viewing 2 replies - 1 through 2 (of 2 total)
  • Solved!

    SELECT * FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    WHERE $wpdb->posts.post_type = 'forum'
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->postmeta.meta_key = '_bbp_topic_count'
    ORDER BY $wpdb->postmeta.meta_value DESC
    LIMIT 100
    ";
    
    $forum_posts = $wpdb->get_results($querystr, OBJECT);
    
    
    if ($forum_posts)
    {
        global $post;
    
        foreach ($forum_posts as $post)
        {
            setup_postdata($post);
            bbp_forum_title();
            the_content();
        }
    

    I dont know why but the first line is missing in the above code?

    $querystr = "
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Skip to toolbar