Skip to:

Sorting activity stream



    is any way to sort buddypress activity stream based on most commented or based on some other property, i have researched and fount (< ;here ) that there is an argument ‘sort’ we can pass to the function ‘bp_has_activities’, but it support only ASC, DESC order of the published date, and i fount an another argument ‘include’ and i have sorted my activity ID’s based on most commented but still buddypress automatically sort the activities based on the ASC or DESC order of the published date.

    can any one help me?

    Thanks in advance

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



    Anybody Please help


    I have added the ‘include’ argument with ‘bp_has_activities’ function as

    <?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ) . '&action=activity_update&include='.$sorted_ids  ) ) : ?>

    where the ‘$sorted_ids’ the comma separated activity id’s in sorted order. But unfortunatly the BuddyPress Core will sort the activities in ASC or DESC order.

    I fount the Query used in core file ‘class-bp-activity-activity.php’ inside ‘plugins\buddypress\bp-activity\classes’ on line number : 585 as

    // Query first for activity IDs.
    $activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}, {$sort}";

    and i have replaced it as

    $activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY FIELD(id,$in)";
    $activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}, {$sort}";

    Now it’s working fine for me, But it’s not recommended to modify the core file

    So please let me know if any one know the filter hook used for modifying the Query from the file itself.



    Hi @subairtc,

    see bp_activity_get_where_conditions filter in bp-activity-classes.php ~L. 418

    Here a similar question with more details.

    Also, maybe you can do something based on this snippet, with a different approach.

     * Change order of activities query string.
     * @param string $query Query string.
     * @return string $query Modified query string.
    function bpfr_filter_activity_default( $query ) {
    	if ( empty( $query ) && !empty( $_POST ) ) {
    		$query = 'order=ASC';
    	return $query;
    add_filter( 'bp_ajax_querystring', 'bpfr_filter_activity_default', 999 );

    Apologize if i’m wrong. 😉


    hi @danbp
    Thanks for your reply,
    i have tried the with


    but this filter will return the where conditions used like

    array(3) {
      string(416) "a.user_id IN ( 451,378,213,431,429,414,415,456,452,473,460,458,283,471,383,468,387,454,441,6,384,475,427 ) AND a.component IN ( 'groups' ) AND a.type IN ( 'activity_update' ) AND a.item_id IN ( 1 )"
      string(13) "a.is_spam = 0"
      string(34) "a.type NOT IN ('activity_comment')"

    it will not contain the ‘ORDER BY’ portion of the query, i hope we will get the total query with the


    filter in ~L, 664, so when we get the entire query we can replace the ‘ORDER BY’ section using string replace, i have tried by using this but i can’t get the query inside the function, do you have any idea how to use this filter ?

    Henry Wright


    do you have any idea how to use this filter ?

    add_filter( 'bp_activity_total_activities_sql', function( $sql, $where_sql, $sort ) {
        // Manipulate $sql here.
        return $sql;
    }, 10, 3 );


    hi @henrywright , @danbp

    Thanks for your great help,

    i have solved the issue without hacking the code using the


    filter in class-bp-activity-activity.php ~L, 609

    the code below is i have used, please refer this thred , where i got some information regarding filters.

    add_filter( 'bp_activity_paged_activities_sql', function( $sql, $where_sql, $sort ) {
    	$pos = strpos($sql, 'WHERE IN');
    		$in_items = implode( ',', $where_sql['in'] );
    		$sql = str_replace('ORDER BY a.date_recorded DESC' ,' ORDER BY FIELD(id,'.$in_items.') ',$sql);
        return $sql;
    }, 10, 3 );

    Thanks again for your valuable time and please let me know any suggestions you have 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Skip to toolbar