Filter Members List Based On Profile Field - Female members are served a directory of males and male members are served a directory of females.
This file contains 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
class BP_Custom_User_Ids { | |
private $custom_ids = array(); | |
public function __construct() { | |
$this->custom_ids = $this->get_custom_ids(); | |
add_action( 'bp_pre_user_query_construct', array( $this, 'custom_members_query' ), 1, 1 ); | |
add_filter( 'bp_get_total_member_count', array( $this, 'custom_members_count' ), 1, 1 ); | |
} | |
private function get_custom_ids() { | |
global $wpdb; | |
// collection based on an xprofile field | |
$custom_ids = $wpdb->get_col("SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 5 AND value = 'Female'"); | |
return $custom_ids; | |
} | |
function custom_members_query( $query_array ) { | |
$query_array->query_vars['include'] = $this->custom_ids; | |
} | |
function custom_members_count ( $count ) { | |
$new_count = count( $this->custom_ids ); | |
return $count - $new_count; | |
} | |
} | |
function custom_user_ids( ) { | |
new BP_Custom_User_Ids (); | |
} | |
add_action( 'bp_before_directory_members', 'custom_user_ids' ); |
This file contains 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
<?php if ( bp_has_members( bp_ajax_querystring( 'members' ) ) ) : ?> |
shanebp
commented
Dec 11, 2013
Aah nevermind. You are a genius. Thank you very much. Theres a bounty for this on stack, question is still unanswered.
How can we pass to get_custom_ids() method parameters from the Members Page?
Hi there, in my site, this code returns the female directory to both genders logging in. But a really cool thing about this code is that it also affects the BuddyPress Members and Recently Active Members widgets, by listing the relevant gender.
Regards
class BP_Custom_User_Ids {
private $custom_ids = array();
public function __construct() {
$this->custom_ids = $this->get_custom_ids();
add_action( 'bp_pre_user_query_construct', array( $this, 'custom_members_query' ), 1, 1 );
add_filter( 'bp_get_total_member_count', array( $this, 'custom_members_count' ), 1, 1 );
}
private function get_custom_ids() {
global $wpdb;
// collection based on an xprofile field
$custom_ids = $wpdb->get_col("SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 5 AND value = 'Female'");
return $custom_ids;
}
function custom_members_query( $query_array ) {
$query_array->query_vars['include'] = $this->custom_ids;
}
function custom_members_count ( $count ) {
$new_count = count( $this->custom_ids );
return $count - $new_count;
}
}
function custom_user_ids( ) {
new BP_Custom_User_Ids ();
}
add_action( 'bp_before_directory_members', 'custom_user_ids' );
I may be a bit dense, but I am unclear as to which buddypress files or wordpress files these two code snippets should be placed? Could you spell it out for me please?
Thank you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment