Skip to:

Adding a new Members Filter or ORDER BY by REPLY count

  • sbmar


    I am looking to add an additional drop-down item on my members listing page that will sort members by their post or more specifically REPLY count in descending order. I have validated that the following SQL works, no I just need it to be able to sort the members in that order:

    SELECT $wpdb->users.ID as ID, COUNT(post_author) as postcount FROM $wpdb->users LEFT JOIN $wpdb->posts ON $wpdb->users.ID = $wpdb->posts.post_author WHERE post_type = 'reply' GROUP BY post_author ORDER BY postcount DESC

    The new drop-down item will be named “Number of Posts”

    Any help to get this to work would be great!

    Thanks, Corey

Viewing 1 replies (of 1 total)

  • sbmar


    Think I finally figured it our for anyone trying to do this… the drop-down item will allow you to view members by the number of bbPress replies…

    // ======================= add order options to members loop ============================
    add_action( 'bp_members_directory_order_options', 'sbmar_add_sorting_options' );
    function sbmar_add_sorting_options() { ?>
    <option value="posts-desc">Number of Posts</option>
    add_action( 'bp_pre_user_query', 'sbmar_pre_user_query' ); 
    function sbmar_pre_user_query( $BP_User_Query ) {
    	// Only run this if one of our custom options is selected
    	if ( in_array( $BP_User_Query->query_vars['type'], array( 'posts-asc', 'posts-desc' ) ) ) {
    		global $wpdb;
    		// Adjust SELECT
    		$BP_User_Query->uid_clauses['select'] = "
    		SELECT $wpdb->posts.post_author AS id
    		FROM $wpdb->posts";
    		// Adjust WHERE
    		$BP_User_Query->uid_clauses['where'] = "WHERE $wpdb->posts.post_type = 'reply'";
    		// Adjust ORDER BY
    		$BP_User_Query->uid_clauses['orderby'] = " GROUP BY $wpdb->posts.post_author ORDER BY COUNT($wpdb->posts.post_author)";
    		// Adjust ORDER
    		$BP_User_Query->uid_clauses['order'] = 'DESC';
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.
Skip to toolbar