Support

Account

Home Forums General Issues ACF in BuddyPress User Loop

Unread

ACF in BuddyPress User Loop

  • Hello. First time posting here. I am a bit a newb with php, so I am looking for guidance on proper use of ACF in this context. Also, I have already posted this question in BuddyPress forums, and Googled extensively, but haven’t gotten a satisfactory answer.

    1.I have an ACF true/false field used on user profiles called ‘new_user’.
    2. I am using BuddyPress and trying to properly pass in the ACF field as a parameter into the BuddyPress Members Loop so that the the member list only displays a list of users with ‘new_user’ value = 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?:

    <!– start loop –>
    <?php if ( bp_has_members() ) : ?>
    <?php endif; ?>
    <?php while ( bp_members() ) : bp_the_member(); ?>
    //OUTPUT MEMBERS LIST HERE
    … <?php endwhile; ?>
    <!– end loop –>
    

    2. 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. Thanks!

Viewing 1 post (of 1 total)

The topic ‘ACF in BuddyPress User Loop’ is closed to new replies.