Skip to:
Content
Pages
Categories
Search
Top
Bottom

Include multiple xProfile fields in members loop


  • fatboyslim1
    Participant

    @fatboyslim1

    I have used this code here:

    https://codex.buddypress.org/developer/loops-reference/the-members-loop/

    and I want to filter users by certain criteria. The example for the bp-custom page is:

    function my_custom_ids( $field_name, $field_value = '' ) {
    
      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 ( $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 = $wpdb->get_col( $query );
    
      if ( !empty( $custom_ids ) ) {
        // convert the array to a csv string
        $custom_ids_str = 'include=' . implode(",", $custom_ids);
        return $custom_ids_str;
      }
      else
       return '';
    
    }

    Example Usage: Get all members who have filled out the xprofile field ‘dogs’ and selected the value ‘poodles’.

    <?php if ( bp_has_members( my_custom_ids( 'dogs', 'poodles' ) ) ) : ?>

    How do I add more conditions to the dogs/poodles. What If I also want ‘birds’, ‘parrot’ as well as ‘dogs’, ‘poodles’ ?

Viewing 14 replies - 1 through 14 (of 14 total)

  • Venutius
    Moderator

    @venutius

    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.


    fatboyslim1
    Participant

    @fatboyslim1

    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.


    Venutius
    Moderator

    @venutius

    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' ) ) ) ) : ?>


    fatboyslim1
    Participant

    @fatboyslim1

    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.


    fatboyslim1
    Participant

    @fatboyslim1

    I think it has something to do with $custom_ids. The value is 1.


    Venutius
    Moderator

    @venutius

    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.


    fatboyslim1
    Participant

    @fatboyslim1

    Yep, it works after changing that. So it seems you were right about array_push not working.


    Venutius
    Moderator

    @venutius

    ok, try replacing that line with the following:

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


    fatboyslim1
    Participant

    @fatboyslim1

    That seems to work! Thanks so much 🙂


    fatboyslim1
    Participant

    @fatboyslim1

    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?


    Venutius
    Moderator

    @venutius

    I’ve editted my original post with added code t remove the current user id from the list.


    fatboyslim1
    Participant

    @fatboyslim1

    I appreciate that, thank you. But the logged in user is still showing.


    Venutius
    Moderator

    @venutius

    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;


    fatboyslim1
    Participant

    @fatboyslim1

    Nice one, thank you!

Viewing 14 replies - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.
Skip to toolbar