Get 15% off on all our plugins, themes and memberships till 4th December. Use coupon: BLACKFRIDAY2021.

BuddyDev

Search

[Resolved] search user by "USER ID"

Tagged: 

  • Participant
    Level: Initiated
    Posts: 14
    swati on #8787

    Is it possible to search user by “USER ID” in member loop ?

  • Keymaster
    (BuddyDev Team)
    Posts: 19833
    Brajesh Singh on #8790

    Hi Swati,
    by user id, you mean the numeric user id or the alphabetic username?

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 14
    swati on #8794

    As the user registered himself/herself, The database creates a unique user ID in numeric form.

  • Keymaster
    (BuddyDev Team)
    Posts: 19833
    Brajesh Singh on #8806

    Hi Swati,
    Thank you for clarifying.

    Please put the following code in your bp-custom.php

    
    
    /**
     * Search a member list by the user id
     */
    function buddydev_search_member_by_user_id( $args ) {
    
    	if ( empty( $args['search_terms'] ) ) {
    		return $args;
    	}
    
    	$term = $args['search_terms'];
    
    	if ( is_numeric( $term ) ) {
    
    		$args['include'] = absint( $term ); // only this user please
    		$args['search_terms'] = ''; // reset search term
    	}
    
    	return $args;
    }
    
    add_filter( 'bp_after_has_members_parse_args', 'buddydev_search_member_by_user_id' );
    
    

    Now if you use numeric id in the directory search form, it will list that user.

    Hope that helps.

  • Participant
    Level: Initiated
    Posts: 14
    swati on #8809

    Hi Brajesh ji,

    Thanks a lot, it’s working…

    I want some more implement in it as in member directory (member-loop.php), I inserted the following code as given below for showing user ID.

    <?php echo “INDIA77”?><?php echo bp_get_member_user_id(); ?>

    // used “INDIA77” as prefix with user id. If the user id is 25, then the result for user id would be INDIA7725.

    But user don’t know that “INDIA77” is prefix, and he would be interested to insert full user id with prefix in search form. so how we can implement this ??

    Thank you

  • Keymaster
    (BuddyDev Team)
    Posts: 19833
    Brajesh Singh on #8814

    Hi Swati,
    Thank you.

    You can use the following code to do that

    
    
    /**
     * Search a member list by the user id with prefixed username
     */
    function buddydev_search_member_by_user_id( $args ) {
    
    	if ( empty( $args['search_terms'] ) ) {
    		return $args;
    	}
    	$prefix = strtolower( "INDIA77" ); // Change the prefix if you are using a different prefix
    
    	$term = strtolower( trim( $args['search_terms'] ) );
    
    	if ( stripos( $term, $prefix )  === 0 ) { //must start with
    		$id = str_replace( $prefix, '', $term );
    		$args['include'] = absint( $id ); // only this user please
    		$args['search_terms'] = ''; // reset search term
    	}
    
    	return $args;
    }
    
    add_filter( 'bp_after_has_members_parse_args', 'buddydev_search_member_by_user_id' );
    
    

    here we are making sure that our search term starts with the “prefix” and then we replace the prefix to find the id. rest is same.

    Hope that helps.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 14
    swati on #8816

    Thanks a lot…

  • Keymaster
    (BuddyDev Team)
    Posts: 19833
    Brajesh Singh on #8902

    You are welcome!

The topic ‘ [Resolved] search user by "USER ID"’ is closed to new replies.

This topic is: resolved