BuddyDev

Search

Array of user IDs for specific member type

  • Participant
    Level: Enlightened
    Posts: 23
    y2gabs on #25402

    Hi there, I need to trail an unrelated URL with all of the user IDs of a specific member type. It needs to look like this – http://www.randomURL.com/URLAction&1,2,3,4,5 (where the numbers at the end are user IDs separated by a comma).

    Can you please advise the best way to do this?

    Thanks so much!!

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #25417

    Hi,
    Thank you for the question. I do not see a direct way to do it currently. BuddyPress does not have an API to fetch all user ids of a member type without fetching their all data(wp user info data). That is not efficient.

    If you still want it, I will post a sample with BP_User_Query.

    Thank you
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 23
    y2gabs on #25437

    Hi Brajesh! thanks so much for your reply here. Would it be possible based on role instead? otherwise yes please post an example described above. Thanks so much!

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #25442

    Hi,
    yes, the role based fetching is very easy.

    Here is a code to get all user ids for subscriber role.

    
    
    	$users_ids = get_users( array(
    		'role'   => 'subscriber',
    		'fields' => 'ID'
    	) );
    

    Please feel free to change the role appropriately.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 23
    y2gabs on #25475

    Thank you sir! How would I add a comma inbetween each ID when calling the variable if using your example above?

  • Keymaster
    (BuddyDev Team)
    Posts: 24623
    Brajesh Singh on #25486

    Hi
    After this line

    
    $users_ids = get_users( array(
    		'role'   => 'subscriber',
    		'fields' => 'ID'
    	) );
    
    

    You can use

    
    
    $list = join( ',', $users_ids );
    
    

    The list will have the comma separated list.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: not resolved