Skip to:
Content
Pages
Categories
Search
Top
Bottom

Order by custom field in member loop


  • ositive
    Participant

    @ositive

    Hi!
    First of all thanks for the amazing work you are doing..
    In my buddypress installation I’ve a rating associated to each member (each member can rate each other) and I would like to add “rating” to the “order by” option of the member page.

    Now I can do it with a widget that orders members with the function I report below. But I would like to add the possibility to order members by rating in the “order by” option.

    I’ve seen the topic https://buddypress.org/support/topic/adding-new-order-by-on-members-loop/ trying to adapt it to my case, but the order by rating option do not work properly.

    It is possible to adapt that solution (or another working) to my case?
    Thanks in advance.

    function prorevs_users_by_rating($limit) {
        global $wpdb;
        if (current_user_can('administrator')):
    
            $users = $wpdb->get_results(
                    $wpdb->prepare(
                            "SELECT
                     a.ID as id, a.user_login AS name, a.display_name AS displayname, AVG(star) AS rating, COUNT(star) AS reviews
                 FROM {$wpdb->prefix}users AS a
                 LEFT JOIN {$wpdb->prefix}bp_activity AS b ON a.ID = b.usercheck
                 WHERE (b.is_activated is null or b.is_activated=1)
                 GROUP BY id
                 ORDER BY rating DESC
                 LIMIT %d", $limit
                    )
            );
        else:
            $users = $wpdb->get_results(
                    $wpdb->prepare(
                            "SELECT
                     a.ID as id, a.user_login AS name, a.display_name AS displayname, AVG(star) AS rating, COUNT(star) AS reviews
                 FROM {$wpdb->prefix}users AS a
                 LEFT JOIN {$wpdb->prefix}bp_activity AS b ON a.ID = b.usercheck
                 WHERE (b.is_activated is null or b.is_activated=1)
                 GROUP BY id
                 ORDER BY rating DESC
                 LIMIT %d", $limit
                    )
            );
        endif;
        return prorevs_print_users($users);
    }
    
    function prorevs_users_by_rating_shortcode($atts) {
        extract(shortcode_atts(array('limit' => 10), $atts));
        return prorevs_users_by_rating($limit);
    }
Viewing 5 replies - 1 through 5 (of 5 total)

  • ositive
    Participant

    @ositive

    In other words, looking to the database, I basically need to add a “sort by” function that sorts users by The avg of a field of the avtivity table. That is, I don’t sort by a x profile field but by the avg of a value of The activity table of my DB. Any idea on how Can i do that, or any piece of code from which i can start to fine The soluto?
    Thanks!!


    ositive
    Participant

    @ositive

    *to find the solution


    ositive
    Participant

    @ositive

    I tryed to work also with this solution (provided by @shanebp too):
    https://buddypress.org/support/topic/creating-a-new-order-by-filter-in-groups-loop-with-custom-sql/
    but I do not obtain to order members by rating.. is this the right approach??

    function antares_members_filter_options() {
        echo '<option value="rating">rating</option>';
    }
    add_action( 'bp_members_directory_order_options', 'antares_members_filter_options' );
    
    function antares_ajax_querystring( $query_string, $object ) {
    
    	if ( 'members' != $object ) 
    		return $query_string;
    
    	if ( ! bp_is_members_directory() ) 
    		return $query_string;
    	
    	$query_args = wp_parse_args( $query_string, array() );
    
            $page = $query_args['page'];
    
    	if( isset( $query_args['action'] ) && $query_args['action'] == 'rating' ) {
    	
    		$query_args = array();
    		$query_args['page'] = $page;
    		$query_args['orderby'] = 'rating';		
    		$query_args['order'] = 'ASC';			
    		$query_args['include'] = antares_get_members(); 
    		$query_string = http_build_query( $query_args );
    
    	}
    
    	return $query_string;
    
    }
    add_filter( 'bp_ajax_querystring', 'antares_ajax_querystring', 32, 2 );
    
    function antares_get_members() {
        global $wpdb;
    
            $sql = "SELECT 
    					a.ID as id, a.user_login AS name, a.display_name AS displayname, AVG(star) AS rating, COUNT(star) AS reviews
    				FROM 
    					{$wpdb->prefix}users AS a
    				LEFT JOIN 
    					{$wpdb->prefix}bp_activity AS b ON a.ID = b.usercheck
    				WHERE 
    					(b.is_activated is null or b.is_activated=1)
    				GROUP BY 
    					id
    				ORDER BY 
    					rating DESC";
        
    $buff = array();
    
    $result = $wpdb->get_results( $sql , OBJECT );
    
    		foreach ($result as $row) {
    			$buff[]= $row->id ;
    		}
    		
    $query_str= implode (',', $buff);
    return $query_str;
    }

    shanebp
    Moderator

    @shanebp


    ositive
    Participant

    @ositive

    Thank you Shanebp
    I tried the following steps (below is the final code):
    -I started from the basic random code
    -I introduced the function to create and pass #sql (like Ninja Warrior trick code)
    but it seems not working.. Can you help me please (I’m really trying everithing..)?
    Thanks

    <?php
     
    // Exit if accessed directly
    if ( !defined( 'ABSPATH' ) ) exit;
     
    class BP_Loop_Filters {
     
        
        public function __construct() {
            $this->setup_actions();
        }
     
        private function setup_actions() {
            /**
             * Adds the Rating order to the select boxes of the Members directory pages
             */
            // Members component is core, so it will be available
            add_action( 'bp_members_directory_order_options', array( $this, 'rating_order' ) );
     
        }
     
        public function rating_order() {
           
    	global $wpdb;
    
    	$sql = "SELECT
                     a.ID as id, a.user_nicename AS name, a.display_name AS displayname, AVG(star) AS rating, COUNT(star) AS reviews
                 FROM {$wpdb->prefix}users AS a
                 LEFT JOIN {$wpdb->prefix}bp_activity AS b ON a.ID = b.usercheck
                 WHERE (b.is_activated is null or b.is_activated=1)
                 GROUP BY id
                 ORDER BY rating DESC";
        var_dump( $sql );
    
    	return $sql;
    
        }
     
    }
     
    function bp_loop_filters() {
        return new BP_Loop_Filters();
    }
     
    add_action( 'bp_include', 'bp_loop_filters' );
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Skip to toolbar