Skip to:
Content
Pages
Categories
Search
Top
Bottom

bp_pre_user_query broken pagination


  • mjHollerATme
    Participant

    @mjholleratme

    Hi Community,

    I am adding a custom filter to the members list and so far it works. I have a custom user meta field “ges_ordername” which usually contains the users last name. Then I have the user roles “subscriber” and “company” and companies are being shown last (or first)

    It works fine with one little culprit: The pagination doesn’t show. It works fine with the default filters but not with this custom one. The pages themselves do work though, because when I manually set $page to 2 it shows the correct offset in the members list.

    add_action( 'bp_pre_user_query', 'mycred_pro_pre_user_query' );
    
    function mycred_pro_pre_user_query( $BP_User_Query ) {
        
        if( ! is_user_logged_in() ) { return; }
        
        if( isset($_GET['bps_form']) && $_GET['bps_form'] !== 'clear' ){
    	    $default_order = "alphabetical";
    	    return;
        }
        
        global $wpdb;
        // Only run this if one of our custom options is selected
        if ( in_array( $BP_User_Query->query_vars['type'], array( 'last-name-companies-after-asc', 'last-name-companies-after-desc', 'last-name-companies-before-asc', 'last-name-companies-before-desc'  ) ) ) {
    
            // Adjust SELECT
            $BP_User_Query->uid_clauses['select'] = "
    SELECT DISTINCT u.{$BP_User_Query->uid_name} as id 
    FROM {$wpdb->users} u 
    INNER JOIN {$wpdb->usermeta} um 
        ON ( u.{$BP_User_Query->uid_name} = um.user_id )
    INNER JOIN {$wpdb->usermeta} um2 
        ON ( u.{$BP_User_Query->uid_name} = um2.user_id )
    INNER JOIN {$wpdb->prefix}bp_xprofile_data bpd
        ON ( u.{$BP_User_Query->uid_name} = bpd.user_id )";
            
            $companies_first = ( in_array( $BP_User_Query->query_vars['type'], array( 'last-name-companies-after-asc', 'last-name-companies-after-desc' ) ) ) ? false : true;
            $companies_first_str = ($companies_first) ? "Firma/Juristische Person" : "Privatperson";
    
            // Adjust WHERE
            $prefix = $wpdb->prefix;
            $BP_User_Query->uid_clauses['where'] = "WHERE (um.meta_key = 'ges_ordername' AND bpd.field_id = '76') AND (um2.meta_key = '{$prefix}capabilities' AND um2.meta_value NOT LIKE '%unbestaetigt%')";
    
            // Adjust ORDER BY
            $BP_User_Query->uid_clauses['orderby'] = "ORDER BY bpd.value = '".$companies_first_str."' DESC, um.meta_value";
    
            // Adjust ORDER
            $BP_User_Query->uid_clauses['order'] = ( $BP_User_Query->query_vars['type'] == 'last-name-companies-after-asc' || $BP_User_Query->query_vars['type'] == 'last-name-companies-before-asc' ) ? 'ASC' : 'DESC';
    
     		$limit = 9;
    
            $BP_User_Query->query_vars['per_page'] = $limit;
    
            $page = $BP_User_Query->query_vars['page'];
    
            $offset = ($page - 1) * $limit;
            $BP_User_Query->uid_clauses['limit'] = "LIMIT " . $offset . ', ' . $limit;
    		
    
        }
    }

    Another question would be, but this doesn’t have priority, how to approach search – because this only works with the default filters for now and therefore I simply disable the custom ones. But this might be for another day, unless someone has a tip for me.

    Thanks in advance for any tips!

Viewing 1 replies (of 1 total)

  • mjHollerATme
    Participant

    @mjholleratme

    ok it seemed to be a caching issue. It actually works.
    Then I am gonna ask about that search. I see there is a

    [“search_terms”]=>
    bool(false)

    in $BP_User_Query, but it is set to false when I was actually searching for sth and $_GET is not empty. Also, when I then go back to the main members list, the “search filters” still appear above the list but $_GET is empty. This is weird behaviour I think in terms of usuability. But other than that I wouldn’t know how I could access that data to modify the query for the search terms?

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.
Skip to toolbar