BP Search Member Types issue

BP Profile Search BP Search Member Types issue

  • Author
    Post
  • #11804 Reply
    Carsten Lund Madsen
    Guest

    Hi Andrea, as suggested I moved this topic over here

    https://wordpress.org/support/topic/is-members-types-pro-single-member-type-field-type-supported/#post-15971385.

    As an alternative to Members Types Pro, I’m trying to add Members Types at registration, using these snippets by @danbp, but I don’t think this is causing it, because the issue was with MTP as well.

    https://buddypress.org/support/topic/2-2-member-types-setting-user-member-types-during-registration-xprofile/

    function using_mt_register_member_types() {
    	bp_register_member_type( 'operator', array(
    		'labels' => array(
    			'name'          => __( 'Operators', 'using-mt' ),
    			'singular_name' => __( 'Operator', 'using-mt' ),
    		),
    	) );
    
    	bp_register_member_type( 'vendor', array(
    		'labels' => array(
    			'name'          => __( 'Vendors', 'using-mt' ),
    			'singular_name' => __( 'Vendor', 'using-mt' ),
    		),
    	) );
    
    	bp_register_member_type( 'coach', array(
    		'labels' => array(
    			'name'          => __( 'Coaches', 'using-mt' ),
    			'singular_name' => __( 'Coach', 'using-mt' ),
    		),
    	) );
    }
    add_action( 'bp_init', 'using_mt_register_member_types' );
    
    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];
    }
    
    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' );
    
    We also need to sort the members list on each type tab using the loop scope.
    
    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 );
    
    And we finally clean the cache to stay up to date with the output
    
    function using_mt_clean_count_cache( $term = 0, $taxonomy = null ) {
    	if ( empty( $term ) || empty( $taxonomy->name ) || 'bp_member_type' != $taxonomy->name )  {
    		return;
    	}
    
    	wp_cache_delete( 'using_mt_count_member_types', 'using_mt_bp_member_type' );
    }
    add_action( 'edited_term_taxonomy', 'using_mt_clean_count_cache', 10, 2 );

    Just to your information, I also had an issue with default value in x-profile fields were not saved after updating. Brajesh from Buddydev discovered this problem, and he kindly updated the BuddyPress function temporary until it is fixed in BP core:

    your issue is persistent cache. You have some plugin/utilities active(either by you or your hosting provider) that provides persistent object caching. I checked the value for

    wp_cache_get( ‘0:4749’, ‘bp_xprofile_data’ )
    and it is set to ‘male’. To be honest, this is partially a bug in BuddyPress where it should have checked for availability of a valid user id before checking cache.

    This is causing the issue. I will be posting on BuddyPress trac about this shortly. I am still not sure how the value for this was set in the cache.

    Regards
    Carsten

    #11806 Reply
    andrea
    Plugin Author

    For other interested readers, the problem disappeared after deactivating and reactivating all plugins on the site, and renaming bp-custom.php. It was probably caused by some redundant code in bp-custom.php itself.

Reply to: BP Search Member Types issue
My Information