Skip to:
Content
Pages
Categories
Search
Top
Bottom

Need help combining bp_has_members() values to customize members loop


  • Mark
    Participant

    @markob17

    Hi,

    I’m having a hard time accomplishing something that is probably really simple but I just don’t have the coding knowledge to figure it out. Basically I want to customize a members loop by combining multiple bp_has_members() functions.

    I’d like to combine <?php if ( bp_has_members( 'type=online' ) ) : ?> and <?php if ( bp_has_members( my_custom_ids( gender, female ) ) ) : ?> so my loop only displays female members who are currently online.

    I am able to get one or the other to work, but can’t figure out how to use both at once. Really appreciate the help.

    Regards!

    mark

Viewing 8 replies - 1 through 8 (of 8 total)
  • // this function should return array with users ids, like this: array(123,3423,21,34231)
    $user_ids = my_custom_ids( 'gender', 'female' );
    
    if ( bp_has_members( array(
    'type' => 'online',
    'include' => $user_ids
    ) ) ) :

    Mark
    Participant

    @markob17

    Hi @slaffik,

    Thank you for helping me. Unfortunately, tried th function and it did not work. Do you have any ideas?

    Regards,

    Mark


    Mark
    Participant

    @markob17

    Anybody else have any ideas as to why this function isn’t working?

    Thanks!

    Please post all your code in pastebin, including my_custom_ids(). And are you sure, that you have any females online?


    Mark
    Participant

    @markob17

    Hi @slaffik,

    The code I am using on in my template is below. I am positive females are online as this is running on my local box and I have multiple female and male testing accounts that I am using for testing.

    <?php 
    $user_ids = my_custom_ids( 'gender', 'female' );
    if ( bp_has_members( array( 'type' => 'online', 'include' => $user_ids ) ) ) : ?>

    Also, I am using the following (taken from the codex) in my functions.php to get these custom ids to work, just in case you need this. And I have verified custom ids are in fact working.

    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 = '" . $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 '';
       
    }

    Really appreciate your help!

    Best Regards,

    Mark

    Please read carefully what I have written in my code section.

    // this function should return array with users ids, like this: array(123,3423,21,34231

    Your my_custom_ids() returns a comma separated string with redundant text, while it should return an array or just comma separated list. Instead of $custom_ids_str = 'include=' . implode(",", $custom_ids); you should have $custom_ids_str = implode(",", $custom_ids); or better just return return $custom_ids;


    Mark
    Participant

    @markob17

    Hi @slaffik,

    Replacing $custom_ids_str = 'include=' . implode(",", $custom_ids); with $custom_ids_str = implode(",", $custom_ids); or return $custom_ids; seems to have resolved my issue! I have to run out the door so I plan on doing more testing when I get home, but so far I believe it is working. After I get home and do some thorough testing I will let you know for sure if the issue is resolved. You rock man, really appreciate your help. Talk soon.

    Regards,
    Mark


    Mark
    Participant

    @markob17

    Hi @slaffik,

    Just got home and did some additional testing. It is working! This ticket can be resolved. Thank you so much for all your help, really appreciate it.

    Best Regards,
    Mark

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Need help combining bp_has_members() values to customize members loop’ is closed to new replies.
Skip to toolbar