BuddyPress:根据XProfile字段有条件地过滤目录

时间:2013-12-08 作者:Androliyah

Members Loop Query

<?php if ( bp_has_members( bp_ajax_querystring( \'members\' ) ) ) : ?>

Question: How can this function be integrated into the members loop so that Female members are served a directory of males and male members are served a directory of females?**

    <?php
     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\' );
Example:

if {current user is "female" (filter members loop to show only males)}
          else if {current user is  "male" (filter loop to show only females)}

3 个回复
最合适的回答,由SO网友:shanebp 整理而成

这是我们讨论的解决方案http://buddypress.org/support/topic/filter-members-list-based-on-profile-field/

它基于http://codex.buddypress.org/developer/bp_user_query/#code-examples

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;

    //figure out if the logged-in user is male or female
   $sex = xprofile_get_field_data( 5, bp_loggedin_user_id() );

   if ( $sex == \'Male\' ) 
      $query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 5 AND value = \'Female\'";
   else 
      $query = "SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 5 AND value = \'Male\'";          


    $custom_ids = $wpdb->get_col( $query );


    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 $new_count;

    }
}

function custom_user_ids( ) {

    new BP_Custom_User_Ids ();

}

add_action( \'bp_before_directory_members\', \'custom_user_ids\' );

SO网友:henrywright

可以使用“include”参数传递逗号分隔的用户ID字符串http://codex.buddypress.org/developer/loops-reference/the-members-loop/

页面上有两个循环,根据每个成员的性别有条件地显示相应的循环。

使用get_users() 函数获取用户ID集:

http://codex.wordpress.org/Function_Reference/get_users

SO网友:Shazzad

//将include替换为user\\u ID

function custom_members_query( $query_array ) {
   // $query_array->query_vars[\'include\'] = $this->custom_ids;
   $query_array->query_vars[\'user_ids\'] = $this->custom_ids;
}


// Also replace this
// $custom_ids = $wpdb->get_col("SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 5 AND value = \'Female\'");

// To
    $curr_user_val = $wpdb->get_var("SELECT value FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 5 AND user_id = ". get_current_user_id() );
    $custom_ids = $wpdb->get_col("SELECT user_id FROM {$wpdb->prefix}bp_xprofile_data WHERE field_id = 5 AND value != \'$curr_user_val\'");

结束

相关推荐

WordPress需要BuddyPress来处理大量用户吗?

我知道从技术上讲18446744073709551615 用户(db限制)。但我在一次演讲中听说,由于缓存问题,WP的普通安装实际上不能很好地处理许多用户,特别是如果添加user\\u meta。有人能证实这一点吗?这位发言者建议,为了让wordpress能够更好地处理大量用户,安装BuddyPress是个好主意,但老实说,代码很乱,文档也很少。所以我正在考虑放弃这一点,再次尝试香草口味。有没有人有任何专业知识,可以阐明这一点?