Skip to:
Content
Pages
Categories
Search
Top
Bottom

Members Loop with ACF Parameter


  • zaino
    Participant

    @zaino

    bp version: latest
    wp version: latest
    site: local

    I am attempting to use an Advanced Custom Field as a parameter on my BP Members loop and I can’t quite get it right.

    The ACF is ‘new_user’, value= true/false, used as a User meta field for administration purposes. To be clear, this is not an xprofile field.

    I am creating a simple admin dashboard where I can display my normal BP Members Loop, but filtered by users who have a true value for the ACF ‘new_user’.

    How exactly do I add the parameter to the following so that my member’s loop only displays users with a value of true for this ACF?

    I have been trying variations on this, and I simply get all users:

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

    I have read the codex, so I am looking for an actual example. Thanks!

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

  • shanebp
    Moderator

    @shanebp

    Please use the code button when posting code.

    Did you read the codex page re the members loop?

    You cannot add custom parameters to the loop.
    You can write a custom query to gather all the member ids that fit your criteria and then pass those ids into the include parameter of the members loop.


    zaino
    Participant

    @zaino

    I have tried this from the codex and it works great for xprofile fields, however I do not know how to modify it for ACF fields. There is a whole block of code in there specific to xprofile fields and I have tried various modifications, none working. Would love to get some concrete guidance. Thanks.

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

    shanebp
    Moderator

    @shanebp

    Please ask questions about ACF on their support forum.
    They may have functions that will return the data that you need.
    If not, they can tell you how to write a custom query for their data.

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