Skip to content

Instantly share code, notes, and snippets.

@danbp
Created October 15, 2016 08:05
Show Gist options
  • Save danbp/9cde6164796768259e741942c2d889b6 to your computer and use it in GitHub Desktop.
Save danbp/9cde6164796768259e741942c2d889b6 to your computer and use it in GitHub Desktop.
BuddyPress - building a member directory by member-type
function using_mt_register_member_types() {
bp_register_member_type( 'ville', array(
'labels' => array(
'name' => __( 'Par Villes', 'textdomain' ),
'singular_name' => __( 'Ville', 'textdomain' ),
),
) );
bp_register_member_type( 'pays', array(
'labels' => array(
'name' => __( 'Par Pays', 'textdomain' ),
'singular_name' => __( 'Pays', 'textdomain' ),
),
) );
bp_register_member_type( 'département', array(
'labels' => array(
'name' => __( 'Par Départements', 'textdomain' ),
'singular_name' => __( 'Département', 'textdomain' ),
),
) );
}
add_action( 'bp_init', 'using_mt_register_member_types' );
// Get the member type count
function using_mt_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];
}
// Display tabs used to filter the members directory by types.
function using_mt_display_directory_tabs() {
$member_types = bp_get_member_types( array(), 'objects' );
// Loop in member types to build the tabs
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'], using_mt_count_member_types( $member_type->name ) ); ?></a>
</li>
<?php endforeach;
}
add_action( 'bp_members_directory_member_types', 'using_mt_display_directory_tabs' );
// Alter the members query arguments to filter the members query by selected type.
function using_mt_set_has_members_type_arg( $args = array() ) {
// Get member types to check scope
$member_types = bp_get_member_types();
// Set the member type arg if scope match one of the registered member type
if ( ! empty( $args['scope'] ) && ! empty( $member_types[ $args['scope'] ] ) ) {
$args['member_type'] = $args['scope'];
}
return $args;
}
add_filter( 'bp_before_has_members_parse_args', 'using_mt_set_has_members_type_arg', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment