Created
May 8, 2012 20:13
-
-
Save boonebgorges/2638943 to your computer and use it in GitHub Desktop.
Limit a bp_has_groups() loop to groups matching a groupmeta key/value pair
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 | |
/** | |
* 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 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 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' ) ); | |
} | |
} | |
?> |
@aaclayton can you please share the code set the scope for the AJAX query of your tab in header as you have mentioned in the comment on this page.it will help lot of developers.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i have tested this code code works good but i am not bale to understand i have select box on group list page like bellow
<select id="groups-order-bydffff"> <option value="user-league">User League</option> <option value="Art and Culture">Art and Culture</option> </select>
if i select Art and Culture i have to show that group which have this meta value so how do send the group name to the loop wanted some thing like this
please any body help me to achieve this using ajax.