You’d have to change the code to accept an array, for example:
<?php if ( bp_has_members( my_custom_ids( array( 'dogs' => 'poodles', 'birds' => 'parrot' ) ) ) ) : ?>
Then for your function you’d do something like this:
function my_custom_ids( $field_array = array() ) {
if ( empty( $field_array ) )
return '';
global $wpdb;
$custom_ids = array();
foreach( $field_array as $field_name => $field_value ) {
$field_id = xprofile_get_field_id_from_name( $field_name );
if ( !empty( $field_id ) )
$query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id;
else
return '';
if ( $field_value != '' )
$query .= " AND value LIKE '%" . $field_value . "%'";
/*
LIKE is slow. If you're sure the value has not been serialized, you can do this:
$query .= " AND value = '" . $field_value . "'";
*/
$custom_ids = array_merge( $custom_ids, $wpdb->get_col( $query ) );
}
$user_id = get_current_user_id();
if ( in_array( $user_id, $custom_ids ) )
unset( $custom_ids[$user_id] );
if ( !empty( $custom_ids ) ) {
// convert the array to a csv string
$custom_ids_str = 'include=' . implode(",", $custom_ids);
return $custom_ids_str;
}
else
return '';
}
Not tested it but that’s the way you’d do it.
Thanks very much for that. I tried it out but it just shows all members even when I change the conditions so something is a bit off. No errors at least.
I guess the first thing to try is to take it back to the basic query and see if that works the same as the original code:
<?php if ( bp_has_members( my_custom_ids( array( 'dogs' => 'poodles' ) ) ) ) : ?>
Thank you again for your response. I did what you suggested and same result, showing all the users and not filtering. When I var_dump the array and the $query the output looks correct. For some reason it just isn’t filtering the users based on these.
I think it has something to do with $custom_ids
. The value is 1.
Maybe it’s the array_push that’s not working, just for testing try replacing:
$custom_ids = array_push( $custom_ids, $wpdb->get_col( $query ) );
With the original@
$custom_ids = $wpdb->get_col( $query );
and keep the query to only dogs.
That should return the same as the original code, but it would only work with one query pair.
Yep, it works after changing that. So it seems you were right about array_push not working.
ok, try replacing that line with the following:
$custom_ids = array_merge( $custom_ids, $wpdb->get_col( $query ) );
That seems to work! Thanks so much 🙂
Oh, one thing I forgot to ask. This is also showing the logged in user. How can I exclude the logged in user from showing up in the list?
I’ve editted my original post with added code t remove the current user id from the list.
I appreciate that, thank you. But the logged in user is still showing.
Try replacing these lines:
$custom_ids_str = 'include=' . implode(",", $custom_ids);
return $custom_ids_str;
with:
`$custom_ids_array = array( ‘include’ => $custom_ids, ‘exclude’ => $user_id );
return $custom_ids_array;