Skip to:

Re-ordering a custom groups loop

  • Herman Aus



    I’ve created a new page template that shows groups with a specific meta value (I call this the group category).

    This is how I call the groups loop in the page template :

    <!-- Category specific groups loop -->
    <?php get_template_part( 'category-specific-groups' ); ?>

    And this is how I customize the groups loop:

    // Get group tag of the page
    $group_tag = get_post_meta(get_the_ID(), 'group-tag', true);
    // Custom Group Query with specified Group Category (Meta query)
    $query_args = array (
        'type' => 'popular',
        'per_page' => 2,	
    $query_args['meta_query'] = array(
            'key'     => 'group-category',
    	'value'   => $group_tag,
    	'compare' => '='
    if ( bp_has_groups( $query_args ) ) :
       while ( bp_groups() ) : bp_the_group();
          // template

    This all works nicely.

    Now I’ve added the Order By filters to the page template and I’d like those options to update the groups query.

    I assume I need to pass the value from the selection to here:

    $query_args = array (
        'type' => '<Order By value>',
        'per_page' => 2,	

    This is how the selection form looks on my page template:

    <select id="groups-order-by">
        <option value="active">Last Active</option>
        <option value="popular">Most Members</option>
        <option value="newest">Newly Created</option>
        <option value="alphabetical">Alphabetical</option>
        <?php do_action( 'bp_groups_directory_order_options' ); ?>

    Any suggestions how to best go about this?



Viewing 4 replies - 1 through 4 (of 4 total)

  • Henry Wright


    Ajax would be my first thought. Check out the AJAX in Plugins article for an introduction to that:

    Herman Aus


    Ay, thanks for that link. It certainly asnwers some of my questions!

    Earlier I tried taking the selected value and storing it using but I couldn’t figure out how to get a working URL and even if I did, how would the groups list get refreshed?

    I figured that BuddyPress has something that already does this. I looked into bp-themes/bp-default/_inc/ajax.php and saw how the bp_ajax_querystring filter is updated there with information from the browser cookies. And I assume that the directory template is reloaded using this function in the same file : bp_dtheme_object_template_loader()

    It’s possible that the value from the select form is already stored but the reason why the template isn’t updating is because this line in ajax.php :

    // Locate the object template
    locate_template( array( "$object/$object-loop.php" ), true );

    can’t locate my custom groups template?

    Henry Wright


    I think the JavaScript will detect a new option list value and kick off a new query to load the new info. I’d imagine you’d need to write the JavaScript listener and the new query yourself but there may be a way of hooking in that I’m not familiar with. Perhaps someone else here can comment?

    Herman Aus


    I looked at the BuddyPress core files global.js and ajax.php in /bp_default/_inc/ and found the function that manages the filter select box.

    Then I fixed the HTML and used the appropriate classes and now the select box works and the groups loop is re-queried (using BuddyPress AJAX) but it reloads the default loop template instead of my modified loop template.

    I also added the search bar to my page using bp_directory_groups_search_form() and that works perfectly. The search results are returned using the modified groups loop.

    Any ideas how I could make the filter select box reload by modified groups loop too?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Re-ordering a custom groups loop’ is closed to new replies.
Skip to toolbar