你想要的是@boone Gages提供的要点中所述的下面的类。该代码用于使用一些与groupmeta匹配的选项卡扩展groups流。
Code:
/**
* This is a quick and dirty class to work around the fact that bp_has_groups() does not have a
* meta_query parameter (or even an \'in\' parameter). Usage:
*
* 1) Just before you fire up your bp_has_groups() loop, instantiate the NG_BP_Groups_Meta_Filter
* class, with parameters $key and $value
* 2) Do your groups loop as normal
* 3) When you\'ve closed the bp_has_groups() loop (endif;), call the method remove_filters() just
* to be safe.
*
* EXAMPLE
* Here\'s how you would run a bp_has_groups() loop that would only show groups that had the meta
* key/value: \'favorite_gum\' => \'Juicy Fruit\'
*
* $meta_filter = new NG_BP_Groups_Meta_Filter( \'favorite_gum\', \'Juicy Fruit\' );
*
* // Note that you can pass whatever arguments you want to bp_has_groups(), as usual
* if ( bp_has_groups() ) :
* while ( bp_groups() ) :
* bp_the_group();
* // Do your template stuff here
* endwhile;
* endif;
*
* // Make sure that other loops on the page are clean
* $meta_filter->remove_filters();
*/
class BP_Groups_Meta_Filter {
protected $key;
protected $value;
protected $group_ids = array();
function __construct( $key, $value ) {
$this->key = $key;
$this->value = $value;
$this->setup_group_ids();
add_filter( \'bp_groups_get_paged_groups_sql\', array( &$this, \'filter_sql\' ) );
add_filter( \'bp_groups_get_total_groups_sql\', array( &$this, \'filter_sql\' ) );
}
function setup_group_ids() {
global $wpdb, $bp;
$sql = $wpdb->prepare( "SELECT group_id FROM {$bp->groups->table_name_groupmeta} WHERE meta_key = %s AND meta_value = %s", $this->key, $this->value );
$this->group_ids = wp_parse_id_list( $wpdb->get_col( $sql ) );
}
function get_group_ids() {
return $this->group_ids;
}
function filter_sql( $sql ) {
$group_ids = $this->get_group_ids();
if ( empty( $group_ids ) ) {
return $sql;
}
$sql_a = explode( \'WHERE\', $sql );
$new_sql = $sql_a[0] . \'WHERE g.id IN (\' . implode( \',\', $group_ids ) . \') AND \' . $sql_a[1];
return $new_sql;
}
function remove_filters() {
remove_filter( \'bp_groups_get_paged_groups_sql\', array( &$this, \'filter_sql\' ) );
remove_filter( \'bp_groups_get_total_groups_sql\', array( &$this, \'filter_sql\' ) );
}
}
来源:
Limit a bp_has_groups() loop to groups matching a groupmeta key/value pair<这个很好用,我已经用过好几次了。要使其正常工作,您必须注意创建其他选项卡,并确保有一个例行程序来处理过滤。可以在链接的课程要点源中找到更多的见解,请仔细查看注释。
此外,我还向您提供了一个我过去如何使用它的示例。下面的代码是一个如何将其与插件提供的组元数据结合使用的示例BuddyPress Group Tags. 过滤器使用的是类别,而不是标记,当然,这只在插件正在使用时有效。
Code:
// create the tabs
add_action( \'bp_groups_directory_group_types\', \'bp_ng_cat_grp_tabs_out\' );
function bp_ng_cat_grp_tabs_out() {
$gtags_categories = get_option( \'gtags_category\' );
$gtags_cat_keys = array_keys( $gtags_categories );
foreach ( $gtags_cat_keys as $g_cat_k ) {
?>
<li class="selected" id="groups-<?php echo $g_cat_k; ?>">
<a href="<?php bp_root_domain() ?>">
<?php $grp_count = count(gtags_get_group_ids( $g_cat_k )); printf( __( \'%s <span>%s</span>\', \'buddypress\' ), $g_cat_k, $grp_count ) ?>
</a>
</li>
<?php
}
}
// take care of filtering
add_action( \'bp_before_groups_loop\', \'bp_ng_cat_grp_tabs_filter\' );
function bp_ng_cat_grp_tabs_filter() {
$grp_querys = bp_ajax_querystring( \'groups\' );
$grp_querys_split = explode( \'&\' , $grp_querys);
$grp_scope = preg_grep( \'#scope=#\' , $grp_querys_split);
if ( $grp_scope != \'\' ) {
$grp_scope = explode( \'=\' , implode( $grp_scope) );
$grp_scope = $grp_scope[1];
}
$gtags_categories = get_option( \'gtags_category\' );
$gtags_cat_keys = array_keys( $gtags_categories );
if ( ! empty( $grp_scope ) && in_array( $grp_scope, $gtags_cat_keys ) ) :
$meta_filter = new BP_Groups_Meta_Filter( \'gtags_group_cat\', $grp_scope );
endif;
}
好的,这应该可以为您提供足够的信息来了解它是如何工作的。当然,这与活动流无关,但正如我所说,你需要这样的东西,我相信这是可能的。我只是从来没有这样做过,所以我不能给你提供一个完整的例子。
正如您所看到的,关键是使SQL适应您的需要,这可以通过可用的过滤器来完成。您可以找到这些以及构建类以扩展活动流所需的几乎所有其他信息bp-activity-classes.php
文件你肯定想看看
bp_activity_get_user_join_filter
和
bp_activity_get_where_conditions
筛选,但还有其他需要考虑的问题,具体取决于您的实际设置。下面的代码提供了一个关于如何使用过滤器来更改活动流/循环的基本示例。按原样,它将替换活动视图,并用元键显示活动favorite_count
并根据元值对结果进行排序。请记住,这不是一个解决方案,我只是在飞行中写下这篇文章,给你更多的洞察力和更好的起点。所以,就照它的意图去做,纯粹是为了测试、学习和理解的目的。
Code:
add_filter( \'bp_activity_get_where_conditions\', \'wpse132109_alter_bp_activity_where_conditions\', 12, 5 );
function wpse132109_alter_bp_activity_where_conditions( $where_conditions, $r, $select_sql, $from_sql, $join_sql ) {
global $bp;
$meta_array = array(
\'relation\' => \'AND\',
array(
\'key\' => \'favorite_count\',
\'compare\' => \'EXISTS\'
)
);
$bp_activity_get_meta_query_sql = BP_Activity_Activity::get_meta_query_sql( $meta_array );
if ( ! empty( $bp_activity_get_meta_query_sql[\'where\'] ) ) {
$where_conditions[] = $bp_activity_get_meta_query_sql[\'where\'];
}
return $where_conditions;
}
add_filter( \'bp_activity_get_user_join_filter\', \'wpse132109_alter_bp_activity_activties_sql\', 12, 6 );
function wpse132109_alter_bp_activity_activties_sql( $sql , $select_sql , $from_sql , $where_sql , $sort , $pag_sql = \'\' ) {
global $bp;
$meta_array = array(
\'relation\' => \'AND\',
array(
\'key\' => \'favorite_count\',
\'compare\' => \'EXISTS\'
)
);
$bp_activity_get_meta_query_sql = BP_Activity_Activity::get_meta_query_sql( $meta_array );
if ( ! empty( $bp_activity_get_meta_query_sql[\'join\'] ) ) {
$join_sql .= $bp_activity_get_meta_query_sql[\'join\'];
}
$sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY sti_bp_activity_meta.meta_value {$sort}";
return $sql;
}
注意:既不是为了生产使用,也不是为了优化,好吧,现在已经很多了,但这肯定会让你开始。简单总结一下:
为您的活动筛选器创建一个类,它与上面显示的类似使用可用的过滤器根据需要更改SQL