0

I am quite new to php and wordpress developement, and I am also new to this website. I want to learn development by trial and error. Please forgive me if i don't use all proper terminology.

I have some code that gets the following id's for a user in buddypress. I would like to use these values in a loop and check the member types of the buddypress user.

I have 3 member types, and the end result should output an array of user ID's per member type.

Below code fetches the user ID's for members being followed by current user.

$following = bp_get_following_ids( $user_id) ; print_r (explode(" ",$following));

Above code gives an output like e.g. : Array ( [0] => 2,3,4,5,6,8,14 ) I would like to use this array in a user query

$user_query = new WP_User_Query( array( 'include' => array( 1, 2, 3 ) ) );

where 1,2,3 should be the output of the code above.

I have tried below, but that seems to be wrong.

$user_query = WP_User_Query( array( 'include' => explode(",",$following)));

How can i get the array into the query so i can create a loop to check the users by member type and output the ID's ? Thanks!

1

This sounds like a job for the wp_parse_id_list() function. It will return an array of unique IDs, sanitized with the absint() function:

$csv = bp_get_following_ids();

if( 0 !== $csv ) )
{
    $uids       = wp_parse_id_list( $csv );
    $user_query = new WP_User_Query( [ 'include' => $uids ] );
}

where we assume that bp_get_following_ids() returns a comma-seperated string of user IDs on success or integer zero on failure.

Example

If we take a comma seperated string, with some "dirty" values:

$csv = "1a,2!,3";

and

$uids  = wp_parse_id_list( $csv );

then

var_dump ( $uids )

array (size=3)
  0 => int 1
  1 => int 2
  2 => int 3

and

print_r( $uids):

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)   
  • Thanks for the help! I can't vote yet, but the help is surely appreciated! – eGuard Sep 29 '15 at 14:23
  • you're welcome, glad to hear it worked for you @eGuard – birgire Sep 29 '15 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.