Creating a new "order by" filter in Groups Loop with custom SQL
-
Hey everyone. Here’s the deal:
I’m trying to add a new “Order by” clause in the filter section of the Groups loop, but not using metadata from buddypress but another plugin table installed in my site. I’ve been looking at this thread for a while https://buddypress.org/support/topic/adding-new-order-by-on-members-loop/ and also this https://codex.buddypress.org/plugindev/add-custom-filters-to-loops-and-enjoy-them-within-your-plugin/#add-a-custom-filter-to-the-members-groups-and-blogs-loops, but none quite work the way I want to, or maybe I’m just missing something because I’m unable to do it.I made this MySQL query:
SELECT g.id as id FROM 'wp_user_league_prediction' as p, 'wp_bp_groups_members' as m, 'wp_bp_groups' as g WHERE g.id = m.group_id AND m.user_id = p.user_id GROUP BY m.group_id
Which queries for groups following the criteria I need (groups whose users are also present in that table referenced there) and I want to use it (or something modified for that matter) to search in the ajax of the Groups Loop filter, so I tried to repurpose the code in the first link like this
function groups_sortby_league( $object ) { global $wpdb, $bp; if ( ! in_array( $object->query_vars['type'], array( 'user_league' ) ) ) return; $field_id = $wpdb->get_var( $wpdb->prepare( "SELECT g.id FROM {$wpdb->prefix}%s_prediction as p, {$wpdb->prefix}bp_groups_members as m, {$wpdb->prefix}bp_groups as g WHERE g.id = m.group_id AND m.user_id = p.user_id GROUP BY m.group_id", $object->query_vars['type'] ) ); $object->gid_name = 'group_id'; $object->gid_table = $bp->group->table_name_data; $object->gid_clauses['select'] = "SELECT u.{$object->gid_name} as id FROM {$object->gid_table} u"; $object->gid_clauses['where'] = " WHERE " . $wpdb->prepare( "u.field_id = %d", $field_id ); $object->gid_clauses['orderby'] = "ORDER BY u.value"; $object->gid_clauses['order'] = "ASC"; } add_action( 'bp_pre_group_query', 'groups_sortby_user_league' );
And while the new filter option does show up, it doesn’t seem like it’s doing anything. I thought the group id would be enough for this but clearly there’s something not quite working and I don’t think it’s the query itself. I don’t know what to do with the $object after that, or what does it do when the $field_id is already referenced.
I really would appreciate some help with this, since this part of buddypress (ajax queries) has always stumped me.
- The topic ‘Creating a new "order by" filter in Groups Loop with custom SQL’ is closed to new replies.