Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Extend bp_members() – Custom Query

  • @zintax

    Participant

    Hello BP Community.

    I’m currently working on a website that uses a combination of WordPress 3.6.1, Buddy Press 1.8, BAUT Pro, WPMU.org’s Membership plugin and an Auction Theme.

    A list of general info about the dynamic of the site:
    – The site will allow users to post items to be auctioned. A niche-specific ebay of sorts.
    – Upon Registration, users can choose to be either an Individual Seller, or a Pawn Shop. Depending on the user_type selected, a user is either assigned a Subscriber or a Contributor level (BAUT Pro does this)
    – If a user registers as a Pawn Shop, they’re later permitted to purchase a monthly subscription plan that will allow them to have a Custom Profile page where they can add a custom header, all their store locations, promotions, and much more. Individual users are not presented with any of this.
    – Finally, there is a DIRECTORY that lists all Pawn shops that have purchased the monthly subscription and links to each of their pages

    This last point is where I have the issue.

    Because I split users in Subscribers and Contributors upon registration, I’m able to use my members-loop.php page to list all Contributors. This gives me an updated list of all users that were registered as Pawn shops, but I would like that SQL Query to also LEFT JOIN with my membership table, where it only lists Contributor level users that have an active subscription.

    My current solution was to add this line at the top of my members-loop.php:

    
    $subscribers = $wpdb->get_results('SELECT  <code>user_id<code>FROM</code>wp_m_membership_relationships<code>WHERE</code>expirydate

    >= NOW( ) ‘ , ARRAY_N );

    .. then, y added the following:

    
         while ( bp_members() ) : bp_the_member();
    	
         if(count($subscribers[0])>0){
    	if (in_array(bp_get_member_user_id(), $subscribers[0])) {
              ...
            }
         }
    
         endwhile;
    

    Essentially I skip any user that is not part of the initial array. However, this doesn’t work since this guy: bp_members_pagination_count() and this guy bp_members_pagination_links() still show the total amount of Contributors/Pawn shops.

    I’ve concluded that the ideal solution would be to try and alter bp_members() only when accessing that specific page so that I may extend the actual MYSQL query, that way I get the correct count and pagination.

    What do you think?
    Am I way off?
    Is there a better way?

Viewing 5 replies - 1 through 5 (of 5 total)
  • @zintax

    Participant

    let me repost the MySql query.. that displayed terrible.

    
    $subscribers = $wpdb->get_results('SELECT  user_id FROM  wp_m_membership_relationships WHERE  expirydate >= NOW( ) ' , ARRAY_N );
    

    @henrywright-1

    Member

    Hey @zintax

    Have you tried hooking to the bp_ajax_querystring filter? This is a good way of modifying both the loop and the pagination.

    Hope this gives you a few ideas:

    
    function modify_loop_and_pag( $qs=false, $object=false ) {
    
        // comma sep list of users to exclude from loop and pagination
        $users = '1,2,3';
     
        if ( $object != 'members' ) // hide for members only
            return $qs;
     
        $args = wp_parse_args( $qs );
     
        if ( !empty( $args['exclude'] ) )
            $args['exclude'] = $args['exclude'] . ',' . $users;
        else
            $args['exclude'] = $users;
     
        $qs = build_query( $args );
     
        return $qs;
    }
    add_action( 'bp_ajax_querystring' , 'modify_loop_and_pag', 25, 2 );
    

    This is a slightly modified version of: http://buddydev.com/buddypress/exclude-users-from-members-directory-on-a-buddypress-based-social-network/

    @shanebp

    Moderator

    @zintax

    Instead of writing new queries to manipulate data after the main user query has run, use this filter to make your adjustments before the main query:
    bp_pre_user_query_construct in bp-core-classes.php

    And run it via a conditional based on the page being viewed.
    Then your pagination data will be what you want.

    @zintax

    Participant

    Thanks @Henry and @Shanebp!

    I ended up using a tweaked version of Henry’s option:

    
    function modify_loop_and_pag( $qs=false, $object=false ) {
         global $wpdb;
    
        if ( $object != 'members' ) // hide for members only
            return $qs;
    
            // create an array with my subscribed users
     	$subscribers = $wpdb->get_results('SELECT  user_id FROM  wp_m_membership_relationships WHERE  expirydate >= NOW( ) ' , ARRAY_N );
    
        $args['include'] = ((!empty( $args['include'] )) ? $args['include'] . ',' : '') . ((empty($subscribers[0])) ? -1 : implode(',',$subscribers[0]));
    
        $qs = build_query($args);
        return $qs;
    }
    add_action( 'bp_ajax_querystring' , 'modify_loop_and_pag', 25, 2 );
    

    Again, thanks for your help.. this opens a number of possibilities.

    Best!

    @zintax

    Participant

    @Henry and @Shanebp, I have a follow up question to this.

    So this query ends up pulling up all the ID’s I want to include in the query, but as I’m getting close to launching my site and I’ve evaluated with my client the estimate of traffic that he expects I’ve come to wonder what the toll on the server will be when I have 5000, 10000 or 15000 users and the list of “include” or “exclude” is too long.

    My biggest concern is that I’m having to do a double query for this. First, I have a query that gives me the IDs to include or exclude and then I pass that list to $qs = build_query($args);

    Is there a way to actually alter the query, not just include/exclude IDs?

    I think it would be easier if I could JOIN the query for subscribers with build_query().

    Any thoughts?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Extend bp_members() – Custom Query’ is closed to new replies.
Skip to toolbar