Created
May 10, 2021 10:08
-
-
Save wbcomdev/540eea802ae6bbca4ae17424e4ab78c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Display member types at member directory tabs. | |
| */ | |
| function wb_member_types_display_directory_tabs() { | |
| $member_types = bp_get_member_types( array(), 'objects' ); | |
| foreach ( $member_types as $member_type ) : | |
| ?> | |
| <li id="members-<?php echo esc_attr( $member_type->name ); ?>"> | |
| <a href="<?php bp_members_directory_permalink(); ?>"><?php printf( '%s <span>%d</span>', $member_type->labels['name'], wb_members_by_count_member_types( $member_type->name ) ); ?></a> | |
| </li> | |
| <?php | |
| endforeach; | |
| } | |
| add_action( 'bp_members_directory_member_types', 'wb_member_types_display_directory_tabs' ); | |
| /** | |
| * Members Counts by Member types. | |
| * | |
| * @param string $member_type Member type. | |
| * @param string $taxonomy Member type taxonomy. | |
| */ | |
| function wb_members_by_count_member_types( $member_type = '', $taxonomy = 'bp_member_type' ) { | |
| global $wpdb; | |
| $member_types = bp_get_member_types(); | |
| if ( empty( $member_type ) || empty( $member_types[ $member_type ] ) ) { | |
| return false; | |
| } | |
| $count_types = wp_cache_get( 'using_mt_count_member_types', 'using_mt_bp_member_type' ); | |
| if ( ! $count_types ) { | |
| if ( ! bp_is_root_blog() ) { | |
| switch_to_blog( bp_get_root_blog_id() ); | |
| } | |
| $sql = array( | |
| 'select' => "SELECT t.slug, tt.count FROM {$wpdb->term_taxonomy} tt LEFT JOIN {$wpdb->terms} t", | |
| 'on' => 'ON tt.term_id = t.term_id', | |
| 'where' => $wpdb->prepare( 'WHERE tt.taxonomy = %s', $taxonomy ), | |
| ); | |
| $count_types = $wpdb->get_results( join( ' ', $sql ) ); | |
| wp_cache_set( 'using_mt_count_member_types', $count_types, 'using_mt_bp_member_type' ); | |
| restore_current_blog(); | |
| } | |
| $type_count = wp_filter_object_list( $count_types, array( 'slug' => $member_type ), 'and', 'count' ); | |
| $type_count = array_values( $type_count ); | |
| if ( empty( $type_count ) ) { | |
| return 0; | |
| } | |
| return (int) $type_count[0]; | |
| } | |
| /** | |
| * Sorting Members by member types. | |
| * | |
| * @param string $args | |
| */ | |
| function wb_sorting_members_by_member_types( $args = array() ) { | |
| $member_types = bp_get_member_types(); | |
| if ( ! empty( $args['scope'] ) && ! empty( $member_types[ $args['scope'] ] ) ) { | |
| $args['member_type'] = $args['scope']; | |
| } | |
| return $args; | |
| } | |
| add_filter( 'bp_before_has_members_parse_args', 'wb_sorting_members_by_member_types', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment