1

PHP newb here, looking for some guidance. I am working with BuddyPress and Advanced Custom Fields (ACF). I have an ACF field 'new_user' with a value of true/false. I am trying to filter my BuddyPress Members Loop to only display users with a value of 'new_user' = true.

There are 2 code samples here.

  1. The standard BP Members Loop. My thought here, is how do I first query my users by ACF ‘new_user’ = true and then start the bp member loop?:
if ( bp_has_members() ) : 
// some code goes here 
endif; 

while ( bp_members() ) : bp_the_member(); 
//OUTPUT MEMBERS LIST HERE
endwhile;
  1. This is a BP function to filter by Buddypress extended user fields. The idea here I believe is to replace the code in the middle specific to xprofile_get_field with the proper ACF code:

    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 '';
    
    }
    

Of course, I am open to solving this in another way as well. I hope this is clear.

mujuonly
  • 11,370
  • 5
  • 45
  • 75
zaino
  • 11
  • 1

0 Answers0