Skip to:
Content
Pages
Categories
Search
Top
Bottom

Member list custom tabs by xprofile fields


  • bentamin
    Participant

    @bentamin

    Hi, I have managed to get a xprofile field (checkbox with three options) to be displayed with a count as tabs next to the “all members” tabs. It’s similar to what @danbp hast done and is working in this Post: 2.2 Member Types – Setting user member types during registration (xProfile)

    Now I am stuck with the filtering of the members-list according to the Tabs when I click on one of the tabs.

    Maybe someone can help me out here.

    Here is the code for creating the tabs so far:

    Function for getting the child values of my checkbox for each Member

    function get_xprofile_child_fields($parent_field_id) {
        global $wpdb;
    
        $child_fields = $wpdb->get_results(
            $wpdb->prepare(
                "SELECT * FROM {$wpdb->prefix}bp_xprofile_fields WHERE parent_id = %d",
                $parent_field_id
            )
        );
    
        return $child_fields;
    }
    

    Function to count how many members have the individual options set

    function get_xprofile_field_member_count($field_id, $field_value) {
        global $wpdb;
    
        $count = $wpdb->get_var(
            $wpdb->prepare(
                "SELECT COUNT(*) FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = %d AND value LIKE %s",
                $field_id,
                '%' . $field_value . '%'
            )
        );
    
        return $count;
    }
    

    Create the tabs with a counter

    function custom_xprofile_child_field_directory_tabs() {
        $parent_field_id = 28; // Replace with your parent xProfile field ID
        
        $child_fields = get_xprofile_child_fields($parent_field_id);
    
        $tab_values = array(); // Store unique field values
    
        if (!empty($child_fields)) {
            foreach ($child_fields as $child_field) {
                $field_id = $child_field->parent_id;
                $field_value = $child_field->name;
    
                // Check if the field value is unique
                if (!in_array($field_value, $tab_values)) {
                    $tab_values[] = $field_value;
    
                    $child_count = get_xprofile_field_member_count($field_id, $field_value);
    
                    // Use a combination of field_id and field_value as the id
                    $tab_id = 'members-' . esc_attr('field_' . sanitize_title($field_value));
    
                    ?>
                    <li id="<?php echo $tab_id; ?>">
                        <a href="<?php echo bp_members_directory_permalink(); ?>"><?php printf( '%s <span>%d</span>', esc_html($field_value), $child_count); ?>
                    </a>
                    </li>
                    <?php
                }
            }
        }
    }
    
    add_action('bp_members_directory_member_types','custom_xprofile_child_field_directory_tabs');

    Thanks for any help on this 🙂

  • You must be logged in to reply to this topic.
Skip to toolbar