Skip to:
Content
Pages
Categories
Search
Top
Bottom

Filter list of profiles on Member Page


  • Myg0t
    Participant

    @myg0t

    Hey guys, I’ve seen this asked here a few times in the past 4 years. But I haven’t seen any working solutions.

    On my installation i’m using buddypress + s2member pro. So far it’s working together nicely. Except when I try to filter profile results on the /members page.

    Honestly, these kinds of manipulations are usually so difficult for me, but for some reason I just can’t figure out what i’m doing wrong.

    in my members-loop.php file here is a snippet of the code i’m using to try and filter the user results. It start directly under the while( bp_members() ) : bp_the_members();

    <?php while ( bp_members() ) : bp_the_member(); ?>

    <?php $user_id = bp_get_member_user_id();

    $user = new WP_User( $user_id );

    if ($user->roles[0] != 's2member_level2')
    continue;
    ?>

    I have also tried this bit of code to get it to work:

    <?php while ( bp_members() ) : bp_the_member(); ?>

    <?php $user=bp_get_member_user_id();

    $s2member_var = get_user_field ("s2member_access_role", $user);

    if ($s2member_var == "s2member_level1" || $s2member_var == "administrator") {

    ?>

    but still no results.

    Using either one does not work, and the members page will still show all the users. I dont understand what i’m doing wrong!

    Thanks so much in advanced for any advice.

    Myg0t

Viewing 8 replies - 1 through 8 (of 8 total)

  • Myg0t
    Participant

    @myg0t

    Ummm… Bump?


    shanebp
    Moderator

    @shanebp

    s2member has functions for looking up the ‘level’ of a logged in user.
    Use their functions rather than WP functions.
    Consult their documentation.
    Or, since you have the Pro version, use their support system.


    Myg0t
    Participant

    @myg0t

    Alright, so here’s my update. I’ve gotten the list of profiles filtered, here is the code i’m using:

    <?php while ( bp_members() ) : bp_the_member(); ?>
     
    	<?php $user=bp_get_member_user_id();
     
    	$s2member_var = get_user_field ("s2member_access_role", $user);
     
    	if ($s2member_var == "s2member_level2") {
     
    ?>

    The only issue remaining now is that the members page is still paginating based on the ~2000 members.

     photo Capture.png

    As should be seen in this snippet.

    I was thinking maybe I could get more support for this question since it’s purely related to buddypress and not S2member?

    Many thanks,

    M


    shanebp
    Moderator

    @shanebp

    >The only issue remaining now is that the members page is still paginating based on the ~2000 members.

    That’s because BP has already collected all the members.
    And then you decide which to display.
    Instead, you should decide which members to collect and then just run the standard bp_members loop.

    Take a look at:

    BP_User_Query

    You’ll have to adjust it to collect all the user ids for s2member_level2.
    They probably have code for that.
    And then change:

    $query_array->query_vars['exclude'] = $this->custom_ids;
    to
    $query_array->query_vars['include'] = $this->custom_ids;

    and change:

    return $count - $new_count;
    to
    return $new_count;

    Myg0t
    Participant

    @myg0t

    Nice, I was actually looking at that a couple of days ago. I had tried a few things out, but to no avail.

    Here’s where i’m at: I’ve commented the above edited section of members-loop.php.

    In my bp-custom.php i’ve copy/pasted the code from the link you sent, and have applied the following edits to it:

        private function get_custom_ids() {
            global $wpdb;
     
            // collection based on an xprofile field
    		// WP_User_Query arguments
    		$args = array (
    			'role'           => 's2member_level2',
    			'fields'         => array( 'id' ),
    		);
    
    		// The User Query
    		$user_query = new WP_User_Query( $args );	
    		//The User Loop
    		if ( ! empty( $user_query->results ) ) {
    			foreach ( $user_query->results as $user ) {
    				$custom_ids = $user.",";
    			}
    		} else {
    			// no users found
    		}
            //$custom_ids = $wpdb->get_col("SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 8 AND value = 'no'");
    		
            return $custom_ids;
        }  

    I also, made the lower two edits you suggested to the $query_array and return $new_count.

    currently i’m getting this error:

    Catchable fatal error: Object of class stdClass could not be converted to string in /home/collegi8/public_html/wp-content/plugins/bp-custom.php on line 30

    line 30 is:

    $custom_ids = $user.",";

    Thanks for all your help man,

    M


    shanebp
    Moderator

    @shanebp

    Try:
    $custom_ids = $user->ID . '','';

    ‘implode’?


    shanebp
    Moderator

    @shanebp

    Yeah, implode is faster.

    But without meta array, the query probably won’t work anyway…
    Read:
    https://codex.wordpress.org/Class_Reference/WP_User_Query#Custom_Field_Parameters

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Filter list of profiles on Member Page’ is closed to new replies.
Skip to toolbar