BuddyDev

Search

filter the buddypress members in the list of members

  • Participant
    Level: Enlightened
    Posts: 126
    Hervé on #12964

    Hi,

    Can I post a request here even if it is not linked to a buddydev plugin?

    I’m trying to filter the buddypress members in the list of members and display them in descending order of precedence on a numeric xprofile field (0 to 10).
    Example of filter xprofile field “I search” = xprofile field “I am”

    I found the function that should work, but I can not do the query:
    `function my_bp_loop_querystring( $query_string, $object ) {
    if ( ! empty( $query_string ) ) {
    $query_string .= ‘&’;
    }
    // $query_string .= ‘per_page=2’; //fonctionne
    $jerecherche = xprofile_get_field_data(‘Je recherche’, $user_id, $multi_format = ‘comma’ );
    $query_string .= ‘search_terms=true&per_page=2’; //ne fonctionne plus en ajoutant un terme

    return $query_string;
    }
    add_action( ‘bp_legacy_theme_ajax_querystring’, ‘my_bp_loop_querystring’, 20, 2 ); `

    • This topic was modified 6 years, 3 months ago by Hervé.
  • Participant
    Level: Enlightened
    Posts: 126
    Hervé on #13012

    Hello,
    A little reminder because I can not do it and it becomes urgent
    Regards

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #13013

    Hi Herve,

    Can you please help me understand this in more details

    
    
    I’m trying to filter the buddypress members in the list of members and display them in descending order of precedence on a numeric xprofile field (0 to 10).
    Example of filter xprofile field “I search” = xprofile field “I am”
    
    

    I am not sure if I understand your requirement clearly.

    1. Is user searching something or are you providing a match based on current logged in user’s xprofile field.

    Thank you
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 126
    Hervé on #13014

    Hello Brajesh

    As in a dating site, each member has an xprofile field like “I am” and an xprofile field like “I’m looking for”
    Example Toto is a man and is looking for a woman
    It is necessary “right” in the query to extract the values ​​of the field I search =”F” (value of the field I am of the other members)
    Regards

  • Participant
    Level: Enlightened
    Posts: 126
    Hervé on #13061

    Hello Brajesh,
    Have I explained better?
    Regards

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #13081

    Hi Herve,

    Here is an example code

    
    
    /**
     * Custom filter for members list for Herve.
     *
     * @param array $args bp_has_members() args.
     *
     * @return array
     */
    function buddydev_filter_members_list_herve( $args ) {
    
    	if ( ! is_user_logged_in() ) {
    		return $args;
    	}
    
    	$user_id = get_current_user_id();
    	// get the value for the field.
    	// Use field id for better performance.
    	$searching_for = xprofile_get_field_data( 'I search', $user_id, 'comma' );
    
    	if ( empty( $searching_for ) ) {
    		return $args;
    	}
    
    	$xprofile_query = isset( $args['xprofile_query'] ) ? $args['xprofile_query'] : array();
    
    	$xprofile_query[] = array(
    		'field'   => 'I am', // I sugget using field id for more efficiency.
    		'value'   => $searching_for,
    		'compare' => '=',
    	);
    
    	$args['xprofile_query'] = $xprofile_query;
    
    	return $args;
    }
    
    add_filter( 'bp_after_has_members_parse_args', 'buddydev_filter_members_list_herve' );
    
    

    Please feel free to modify and adapt it as you please.

    Best regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 126
    Hervé on #13099

    Hi Brajesh,

    Thank you for this tips.
    I put the name of my fields but it does not work. So I put the field_id, it does not work, but maybe I was wrong !?

    function buddydev_filter_members_list_herve( $args ) {
    
    	if ( ! is_user_logged_in() ) {
    		return $args;
    	}
    
    	$user_id = get_current_user_id();
    	// get the value for the field.
    	// Use field id for better performance.
    	
    	$field_searching_for = '42'; // the ID of the field referenced in table wp_bp_xprofile_fields
    $searching_for = xprofile_get_field_data( $field_searching_for, $user_id );
    
    	if ( empty( $searching_for ) ) {
    		return $args;
    	}
    
    	$xprofile_query = isset( $args['xprofile_query'] ) ? $args['xprofile_query'] : array();
    
    $field_jesuis = '41'; // the ID of the field referenced in table wp_bp_xprofile_fields
    
    	$xprofile_query[] = array(
    		'field'   => $field_jesuis, // I sugget using field id for more efficiency.
    		'value'   => $searching_for,
    		'compare' => '=',
    	);
    
    	$args['xprofile_query'] = $xprofile_query;
    
    	return $args;
    }
    
    add_filter( 'bp_after_has_members_parse_args', 'buddydev_filter_members_list_herve' );

    Does the type of field (radio button, radio button, select) have an impact on this filter function!?

    I also want to sort descending on the xprofile digital field “priority”
    I found the filter “add_filter( ‘bp_ajax_querystring …” on https://codex.buddypress.org/developer/function-examples/bp_ajax_querystring/ . Is not it easier to manage before displaying a theme?

    Regards

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #13117

    Hi Herve,
    My mistake. I forgot that bp_has_members() won’t use xprofile_query instead it should be specified on BP_User_Query

    Here is the updated code

    
    
    /**
     * Custom filter for members list for Herve.
     *
     * @param BP_User_Query $query query object.
     *
     * @return array
     */
    function buddydev_filter_members_list_herve( $query ) {
    
    	if ( ! is_user_logged_in() ) {
    		return;
    	}
    
    	$user_id = get_current_user_id();
    	// get the value for the field.
    	// Use field id for better performance.
    	$searching_for = xprofile_get_field_data( 'I search', $user_id, 'comma' );
    
    	if ( empty( $searching_for ) ) {
    		return;
    	}
    
    	$xprofile_query = isset( $query->query_vars['xprofile_query'] ) ? $query->query_vars['xprofile_query'] : array();
    
    	$xprofile_query[] = array(
    		'field'   => 'I am', // I sugget using field id for more efficiency.
    		'value'   => $searching_for,
    		'compare' => '=',
    	);
    
    	if ( ! empty( $xprofile_query ) ) {
    		$query->query_vars['xprofile_query'] = $xprofile_query;
    	}
    
    	return $query;
    }
    
    add_action( 'bp_pre_user_query_construct', 'buddydev_filter_members_list_herve', 0 );
    

    Replace the field with actual field is/name and it will work. Please make sure to remove the old code.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 126
    Hervé on #13125

    Hi Brajesh
    I just did a quick test and it works 🙂
    I will replace by the field id to optimize!
    Is it easy to do a descending sort on a numerc (0 to 10) xprofile at the same time?
    thank you very much

  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #13126

    HI Herve,
    Glad that it worked.

    If the value is stored in usermeta, It is certainly possible to order the list but using xprofile field data will need some sql manipulations and I am not much in favour of it.

    Regards
    Brajesh

The topic ‘filter the buddypress members in the list of members’ is closed to new replies.

This topic is: not resolved