0

I want to loop members where profile custom fields has a condition, let me explain, I have latitude and longitude as custom fields on members buddypress site, and I need to loop just members who has set latitude and longitude, using bp_has_member

if ( bp_has_members( 'per_page=0' ) ) : ?>
endif;

I have tried with ( "Dirección" is my custom xprofile that I am using in order to filter member with no address ) however that doesn't work, or something is wrong.

bp_has_members( 'per_page=0&meta_key=Dirección&meta_value!=\'\'' )

I have been trying on many ways without any result

closed as off-topic by Johannes Pille, Nicolai, gmazzap, birgire, Pieter Goosen Jul 31 '14 at 18:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – Johannes Pille, Nicolai, gmazzap, birgire, Pieter Goosen
If this question can be reworded to fit the rules in the help center, please edit the question.

  • It seems you are providing the wrong meta key and value – Maruti Mohanty Jan 22 '14 at 7:25
3
+50

Buddypress' bp_has_members() function accepts several parameters to alter the output. Take a look at the Accepted Parameters section on the codex page regarding the members loop. The two parameter meta_key and meta_value might just be what you're looking for. The Code Examples section should give you an additional insight in how to do it - correct.


Update:

Firstly, you can't use the »not equal | !=« operator like you've done in your comment.

Secondly, @MarutiMohanty might be right about the use of the correct values for the parameters. You might want to debug your usermeta, you can use below code to output this information on your members loop page - it goes into your functions.php.

Code:

add_action( 'bp_before_members_loop', 'wpse129106_usermeta_debug' );
function wpse129106_usermeta_debug() {
    $user_id = get_current_user_id();
    $all_meta_for_user = get_user_meta( $user_id );
    echo '<pre>';
    print_r( $all_meta_for_user );
    echo '</pre>';
}


Thirdly, after giving this situation a really quick test, I can say that all the below shown approaches are just working fine for me.

Code:

// query string, only key - works
$qs_args_key = 'per_page=0&meta_key=uniquekey';

// query string, key and value - works
$qs_args_key_and_value = 'per_page=0&meta_key=uniquekey&meta_value=uniquevalue';

// arguments array, key and value - works
$args_array = array(
    'per_page' => 0,
    'meta_key' => 'uniquekey',
    'meta_value' => 'uniquevalue'
);

// replace the variable to test the options
if ( bp_has_members( $args_array ) ) : 

2nd Update:

As you want to have the ability to compare the meta value you have do some additional work. This is pretty much following the pattern the buddypress codex example I referred you to above suggests.
In short, read the explanation at the codex page for details, we create a function that does the comparing and filters the ids accordingly, with that we make use of the include parameter the bp_has_members() has. I adapted the suggested example a bit, firstly by making a third argument - $compare - possible and secondly by changing up the conditional responsible for the SQL statement, that handles the comparing option. The code goes into your functions.php or bp-custom.php.

Code:

function wpse129106_bp_member_ids_by_field( $field_name, $field_value = '', $compare = '=' ) {
    if ( empty( $field_name ) )
        return '';

    global $wpdb;

    $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 ( !empty( $query ) ) {    
        if ( $compare == 'LIKE' || $compare == 'NOT LIKE' ) {
            $query .= " AND value " . $compare . " '%" . $field_value . "%'";
        } else if ( $compare == '=' && $field_value == '' ) {
            $query = "SELECT DISTINCT user_id FROM " . $wpdb->prefix . "bp_xprofile_data";
            $query .= " WHERE user_id NOT IN";
            $query .= " (SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id . " AND value != '')";
        } else {
            $query .= " AND value " . $compare . " '" . $field_value . "'";
        }
    } else {
        return '';
    }

    $custom_ids = $wpdb->get_col( $query );

    if ( !empty( $custom_ids ) ) {
        $custom_ids_str = 'include=' . implode(",", $custom_ids);
        return $custom_ids_str;
    } else {
        return ''; 
    }
}

You can use it like in this example to alter your members loop:

if ( bp_has_members( 
         'per_page=0&' . 
         wpse129106_bp_member_ids_by_field(
             'First Name',
             'yourvalue',
             '!=' 
         ) 
     ) 
   ) :
  • I have tried with "bp_has_members( 'per_page=0&meta_key=Dirección&meta_value!=\'\'' )" however that doesn't work. COuld you give me a hand. My custom field in order to look for no empty or no filled is "Dirección" – Carlos Jan 13 '14 at 19:42
-1

For looping through the members such that they have a custom xprofile data , create an if block inside the member_loop.

Code :-

<?php if ( bp_has_members() ) : ?>
 <?php while ( bp_members() ) : bp_the_member(); $userId = bp_get_member_user_id() ?>
  <?php $user = new WP_User( $userId );
  if(xprofile_get_field_data( 'Dirección' , $userId ) != '' ) : ?>

   <?php bp_member_name();//Here is where you will have the loop ?>

  <?php endif; ?>
 <?php endwhile; ?>
<?php endif; ?>
  • While it could be done this way, it really shouldn't, because you're collecting data you don't need and just skip the output. This absolutely isn't recommendable, because especially on larger sites this could lead performance issues or at the very least will be cutting off performance you don't need to invest. – Nicolai Jan 24 '14 at 12:07

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